home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_140 / sbprolog / sbprolog.doc < prev    next >
Text File  |  1992-05-06  |  158KB  |  6,997 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                    The SB-Prolog System, Version 2.2
  15.  
  16.                              A User Manual
  17.  
  18.                                edited by
  19.                             Saumya KDebray
  20.  
  21.                             from material by
  22.  
  23.                            David Scott Warren
  24.                             Suzanne Dietrich
  25.                           SUNY at Stony Brook
  26.  
  27.                             Fernando Pereira
  28.                            SRI International
  29.  
  30.  
  31.  
  32.                                March 1987
  33.  
  34.  
  35.                      Department of Computer Science
  36.                          University of Arizona
  37.                             Tucson, AZ 85721
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.                    The SB-Prolog System, Version 2.2
  82.  
  83.                              A User Manual
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.       Abstract: SB-Prolog is a  public-domain  Prolog  system  for
  94.  
  95.       Unix-  based  systems  originally  developed  at SUNY, Stony
  96.  
  97.       Brook.  The core of the system is an emulator, written in  C
  98.  
  99.       for  portability,  of  a  Prolog  virtual machine that is an
  100.  
  101.       extension of the Warren Abstract Machine.  The remainder  of
  102.  
  103.       the system, including the translator from Prolog to the vir-
  104.  
  105.       tual machine instructions, is written in Prolog.   Parts  of
  106.  
  107.       this  manual, specifically the sections on Prolog syntax and
  108.  
  109.       descriptions of some of the builtins, are based  on  the  C-
  110.  
  111.       Prolog User Manual by Fernando Pereira.
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.       ____________________
  122.          - Unix is a trademark of AT&T.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. 1.  Introduction
  142.  
  143.  
  144.  
  145.  
  146.      SB-Prolog is a public domain Prolog system based on  an  extension  of
  147.  
  148. the Warren Abstract Machine[1].  The WAM  simulator  is  written  in  C  to
  149.  
  150. enhance portability.  Prolog source programs can be compiled into byte code
  151.  
  152. files, which contain encodings of WAM instructions and are  interpreted  by
  153.  
  154. the simulator.  Programs can also be interpreted via consult.
  155.  
  156.  
  157.      SB-Prolog offers several features that are not found  on  most  Prolog
  158.  
  159. systems  currently  available.  These include: compilation to object files;
  160.  
  161. dynamic loading of predicates; provision for generating executable code  on
  162.  
  163. the  global  stack,  which  can  be  later be reclaimed; an extension table
  164.  
  165. facility that permits memoization of  relations.   Other  features  include
  166.  
  167. full  integration between compiled and interpreted code, and a facility for
  168.  
  169. the definition and expansion of macros that is fully  compatible  with  the
  170.  
  171. runtime system.
  172.  
  173.  
  174.      The following features are absent: we expect to  incorporate  them  in
  175.  
  176. future versions.
  177.  
  178.  
  179.      A ``listing'' feature, which produces a listing of clauses in the sys-
  180.  
  181.      tem database.
  182.  
  183.  
  184.      A garbage collector for the global stack.
  185.  
  186. ____________________
  187.    [1] D. H. D. Warren, ``An Abstract Prolog Instruction Set'', Tech.  Note
  188. 309, SRI International, 1983.
  189.  
  190.  
  191.  
  192.  
  193.  
  194.                                      1
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.      The            record/recorded/erase,            clause            and
  208.  
  209.      current_atom/current_functor/current_predicate facilities of C-Prolog.
  210.  
  211.  
  212.      In addition, it should be mentioned that while  interpreted  and  com-
  213.  
  214. piled code behave similarly in almost all cases, there are a few cases that
  215.  
  216. we are aware of where they behave differently:
  217.  
  218.  
  219. (1)  In cuts buried within if-then-elses (->): such  cuts  are  treated  as
  220.  
  221.      ``hard''  cuts in interpreted code (i.e. cuts away alternative clauses
  222.  
  223.      as well), but as ``soft'' cuts in compiled code (i.e. cuts as far back
  224.  
  225.      as the head of the clause, but does not cut away alternative clauses).
  226.  
  227.  
  228. (2)  In the evaluation of arithmetic expressions involving compound  subex-
  229.  
  230.      pressions  created  dynamically: in compiled code, this cannot be han-
  231.  
  232.      dled using is/2, and must be handled using eval/2 (see the section  on
  233.  
  234.      Arithmetic  predicates),  while  in  interpreted  code  either is/2 or
  235.  
  236.      eval/2 is acceptable.
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.                                      2
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. 2.  Getting Started
  274.  
  275.  
  276.  
  277.  
  278.      This section is intended to give a broad  overview  of  the  SB-Prolog
  279.  
  280. system,  so  as  to  enable  the  new user to begin using the system with a
  281.  
  282. minimum of delay.  Many of the  topics  touched  on  here  are  covered  in
  283.  
  284. greater depth in later sections.
  285.  
  286.  
  287. 2.1.  The Dynamic Loader Search Path
  288.  
  289.  
  290.  
  291.  
  292.      In SB-Prolog, it is not necessary for the user to load all the  predi-
  293.  
  294. cates  necessary  to execute a program.  Instead, if an undefined predicate
  295.  
  296. foo is encountered during execution, the system searches the user's  direc-
  297.  
  298. tories  in the order specified by the environment variable SIMPATH until it
  299.  
  300. finds a directory containing a file foo whose name is that of the undefined
  301.  
  302. predicate.   It  then  dynamically  loads  and links the file foo (which is
  303.  
  304. expected to be a byte code file defining the predicate foo), and  continues
  305.  
  306. with execution; if no such file can be found, an error message is given and
  307.  
  308. execution fails.  This feature makes it unnecessary for the user to have to
  309.  
  310. explicitly link in all the predicates that might be necessary in a program:
  311.  
  312. instead, only those files are loaded which are necessary to have  the  pro-
  313.  
  314. gram  execute.   This  can  significantly reduce the memory requirements of
  315.  
  316. programs.
  317.  
  318.  
  319.      The key to this  dynamic  search-and-load  behaviour  is  the  SIMPATH
  320.  
  321. environment variable, which specifies the order in which directories are to
  322.  
  323. be searched.  It may be set by adding the  following  line  to  the  user's
  324.  
  325.  
  326.                                      3
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339. .cshrc file:
  340.  
  341.  
  342.  
  343.             setenv SIMPATH path
  344.  
  345.  
  346.  
  347.  
  348. where path is a sequence of directory names separated by colons:
  349.  
  350.  
  351.  
  352.             dir<1>:dir<2>: ... :dir<n>
  353.  
  354.  
  355.  
  356.  
  357. and dir<i> are full path names to the respective directories.  For example,
  358.  
  359. executing the command
  360.  
  361.             setenv SIMPATH .:$HOME/prolog/modlib:$HOME/prolog/lib
  362.  
  363.  
  364.  
  365.  
  366. sets the search order for undefined predicates to the following: first, the
  367.  
  368. directory in which the program is executing is searched; if the appropriate
  369.  
  370. file is not found in this  directory,  the  directories  searched  are,  in
  371.  
  372. order,  ~/prolog/modlib  and  ~/prolog/lib.  If the appropriate file is not
  373.  
  374. found in any of these directories, the system gives an  error  message  and
  375.  
  376. execution fails.
  377.  
  378.  
  379.      The beginning user  is  advised  to  include  the  system  directories
  380.  
  381. (listed  in the next section) in his SIMPATH, in order to be able to access
  382.  
  383. the system libraries (see below).
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.                                      4
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405. 2.2.  System Directories
  406.  
  407.  
  408.  
  409.  
  410.      There are four basic system directories: cmplib, lib, modlib and  sim.
  411.  
  412. cmplib  contains the Prolog to byte code translator; lib and modlib contain
  413.  
  414. library routines.  The src subdirectory  in  each  of  these  contains  the
  415.  
  416. corresponding Prolog source programs.  The directory sim contains the simu-
  417.  
  418. lator, the subdirectory builtin contains code for the builtin predicates of
  419.  
  420. the system.
  421.  
  422.  
  423.      It is recommended that the beginning user include  the  system  direc-
  424.  
  425. tories in his SIMPATH, by setting SIMPATH to
  426.  
  427.                       .:SBP/modlib:SBP/lib:SBP/cmplib
  428.  
  429.  
  430. where SBP denotes the path to the root of the SB-Prolog system directories.
  431.  
  432.  
  433. 2.3.  Invoking the Simulator
  434.  
  435.  
  436.  
  437.  
  438.      The simulator is invoked by the command
  439.  
  440.             sbprolog bc_file
  441.  
  442.  
  443. where bc_file is a byte code file resulting from the compilation of a  Pro-
  444.  
  445. log  program.  In almost all cases, the user will wish to interact with the
  446.  
  447. SB-Prolog query evaluator, in which case bc_file will be $readloop, and the
  448.  
  449. command will be
  450.  
  451.     sbprolog PATH/\$readloop
  452.  
  453.  
  454. where PATH is the path to the directory containing the command  interpreter
  455.  
  456.  
  457.  
  458.                                      5
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471. $readloop.  This directory, typically, is modlib (see Section 2.2 above).
  472.  
  473.  
  474.      The command interpreter reads in a query typed in by the user,  evalu-
  475.  
  476. ates  it  and  prints  the answer(s), repeating this until it encounters an
  477.  
  478. end-of-file (the standard end-of-file character on the system,  e.g.  ctrl-
  479.  
  480. D), or the user types in end_of_file or halt.
  481.  
  482.  
  483.      The user should ensure that the the directory containing  the  execut-
  484.  
  485. able  file sim (typically, the system directory sim: see Section 2.2 above)
  486.  
  487. is included in the shell variable path; if not, the full path to the  simu-
  488.  
  489. lator will have to be specified.
  490.  
  491.  
  492.      In general, the simulator may be invoked with a variety of options, as
  493.  
  494. follows:
  495.  
  496.              sbprolog -options bc_file
  497.     or
  498.              sbprolog -option<1> -option<2> ... -option<nbc_file
  499.  
  500.  
  501. The options recognized by the simulator are described in Section 4.2.
  502.  
  503.  
  504.      When called with a byte code file bc_file, the simulator begins execu-
  505.  
  506. tion  with the first clause in that file.  The first clause in such a file,
  507.  
  508. therefore, should be a clause without any arguments in the head (otherwise,
  509.  
  510. the  simulator  will  attempt  to dereference argument pointers in the head
  511.  
  512. that are really pointing into deep space, and usually come to a  sad  end).
  513.  
  514. If  the  user is executing a file in this manner rather than using the com-
  515.  
  516. mand interpreter, he should also be careful to include the undefined predi-
  517.  
  518. cate  handler  `_$undefined_pred'/1,  which is normally defined in the file
  519.  
  520. modlib/$init_sys.P.
  521.  
  522.  
  523.  
  524.                                      6
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537. 2.4.  Executing Programs
  538.  
  539.  
  540.  
  541.  
  542.      There are two ways of executing a program: a source file may  be  com-
  543.  
  544. piled into a byte-code file, which can then be loaded and executed; or, the
  545.  
  546. source file may be interpreted  via  consult.   The  system  supports  full
  547.  
  548. integration  of compiled and interpreted code, so that some predicates of a
  549.  
  550. program may be compiled, while others may  be  interpreted.   However,  the
  551.  
  552. unit  of compilation or consulting remains the file.  The remainder of this
  553.  
  554. section describes each of these procedures in more detail.
  555.  
  556.  
  557. 2.4.1.  Compiling Programs
  558.  
  559.  
  560.  
  561.  
  562.      The compiler is invoked through  the  Prolog  predicate  compile.   It
  563.  
  564. translates  Prolog source programs into byte code that can then be executed
  565.  
  566. on the simulator.  The compiler may be invoked as follows:
  567.  
  568.  
  569.  
  570.             | ?- compile(InFile [, OutFile ] [, OptionsList ]).
  571.     or
  572.             | ?- compile(InFileOutFileOptionsListPredList).
  573.  
  574.  
  575.  
  576.  
  577. where optional parameters are enclosed in brackets.  InFile is the name  of
  578.  
  579. the  input (i.e. source) file; OutFile is the name of the output file (i.e.
  580.  
  581. byte code) file; OptionsList is a list of compiler options, and PredList is
  582.  
  583. a list of terms P/N denoting the predicates defined in InFile, where P is a
  584.  
  585. predicate name and N its arity.
  586.  
  587.  
  588.  
  589.  
  590.                                      7
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.      The input and output file names must  be  Prolog  atoms,  i.e.  either
  604.  
  605. begin  with a lower case letter and consist only of letters, digits, dollar
  606.  
  607. signs and underscores; or, be enclosed within single quotes.  If the output
  608.  
  609. file  name  is  not  specified,  it  defaults  to  InFile.out.  The list of
  610.  
  611. options, if specified, is a Prolog list, i.e. a term of the form
  612.  
  613.  
  614.  
  615.             [ option<1>, option<2>, ..., option<n> ].
  616.  
  617.  
  618.  
  619.  
  620. If left unspecified, it defaults to the empty list [].  PredList, if speci-
  621.  
  622. fied,  is usually given as an uninstantiated variable; its principal use is
  623.  
  624. for setting trace points on the predicates in the file (see Sections 6  and
  625.  
  626. 8).  Notice that PredList can only appear in compile/4.
  627.  
  628.  
  629.      A list of compiler options appears in Section 8.3.
  630.  
  631.  
  632. 2.4.2.  Loading Byte Code Files
  633.  
  634.  
  635.  
  636.  
  637. Byte code files may be loaded into the simulator using the predicate load:
  638.  
  639.  
  640.  
  641.             | ?- load(ByteCode_File).
  642.  
  643.  
  644.  
  645.  
  646. where ByteCode_File is a Prolog atom (see Section 3.1) that is the name  of
  647.  
  648. a byte code file.
  649.  
  650.  
  651.      The load predicate invokes the dynamic loader,  which  carries  out  a
  652.  
  653. search  according  to  the  sequence  specified by the environment variable
  654.  
  655.  
  656.                                      8
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669. SIMPATH (see Section 2.1).  It is therefore not necessary to always specify
  670.  
  671. the full path name to the file to be loaded.
  672.  
  673.  
  674.      Byte code files may be concatenated together  to  produce  other  byte
  675.  
  676. code  files.   Thus,  for  example,  if  foo1  and foo2 are byte code files
  677.  
  678. resulting from the compilation of two Prolog source programs, then the file
  679.  
  680. foo, obtained by executing the shell command
  681.  
  682.              cat foo1 foo2 > foo
  683.  
  684.  
  685. is a byte code file as well, and may be loaded and executed.  In this case,
  686.  
  687. loading  and  executing  the file foo would give the same result as loading
  688.  
  689. foo1 and foo2 separately, which in turn would be the same as  concatenating
  690.  
  691. the  original  source  files and compiling this larger file.  This makes it
  692.  
  693. easier to compile large programs: one need only  break  them  into  smaller
  694.  
  695. pieces,  compile  the individual pieces, and concatenate the resulting byte
  696.  
  697. code files together.
  698.  
  699.  
  700. 2.4.3.  Consulting Programs
  701.  
  702.  
  703.  
  704.  
  705.      Instead of compiling a file to generate a byte code  file  which  then
  706.  
  707. has  to  be  loaded, a program may be executed interpretively by ``consult-
  708.  
  709. ing'' the corresponding source file:
  710.  
  711.             | ?- consult(SourceFile [, OptionList ] ).
  712.     or
  713.             | ?- consult(SourceFileOptionListPredList).
  714.  
  715.  
  716. where SourceFile is a Prolog atom which is the name of a file containing  a
  717.  
  718. Prolog  source  program;  OptionList  is  a list of options to consult; and
  719.  
  720.  
  721.  
  722.                                      9
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735. PredList is a list of terms P/N, where P is a  predicate  name  and  N  its
  736.  
  737. arity, specifying which predicates have been consulted from SourceFile; its
  738.  
  739. principal use is for setting trace points on the  predicates  in  the  file
  740.  
  741. (see Section 6).  Notice that PredList can only appear in consult/3.
  742.  
  743.  
  744.      At this point, the options recognized for consult are the following:
  745.  
  746.  
  747. first
  748.  
  749.  
  750.      If on, causes each clause read in to be added as the first  clause  of
  751.  
  752.      the  database  (so that effectively, the clauses appear in the reverse
  753.  
  754.      order as in the source file).  Default: off.
  755.  
  756.  
  757. index
  758.  
  759.  
  760.      If on, causes an index to be generated on the first argument  of  each
  761.  
  762.      predicate.  Default: off.
  763.  
  764.  
  765. index(N)
  766.  
  767.      If on, causes an index to be generated on the N[th] argument  of  each
  768.  
  769.      predicate.  Default: off.
  770.  
  771.  
  772. q    ``quick''.  If on, invokes a consultation algorithm  that  is  simpler
  773.  
  774.      and  more efficient than the general one.  However, the code generated
  775.  
  776.      with the simpler algorithm may not be correct if  there  are  repeated
  777.  
  778.      variables within compound terms in the body of the clause, e.g. in
  779.  
  780.                  p(X) :- q([X, X]).
  781.  
  782.  
  783.      Default: off.
  784.  
  785.  
  786.  
  787.  
  788.                                     10
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801. t    ``trace''.  Causes a trace point to be set on  any  predicate  in  the
  802.  
  803.      current file that does not already have a trace point set.
  804.  
  805.  
  806. v    ``verbose''.  Causes information regarding which predicates have  been
  807.  
  808.      consulted to be printed out. Default: off.
  809.  
  810.  
  811. In addition to the above, the options specified for the macro expander  are
  812.  
  813. also recognized (see Section 10)).
  814.  
  815.  
  816.      It is important to note that SB-Prolog's consult predicate is  similar
  817.  
  818. to  that  of  Quintus  Prolog, and behaves like C-Prolog's reconsult.  This
  819.  
  820. means that if a predicate is defined across two or more  files,  consulting
  821.  
  822. them will result in only the clauses in the file consulted last being used.
  823.  
  824.  
  825. 2.5.  Execution Directives
  826.  
  827.  
  828.  
  829.  
  830.      Execution directives may be specified to compile and  consult  through
  831.  
  832. :-/1.   If,  in the read phase of compile or consult, a term with principal
  833.  
  834. functor :-/1 is read in, this term is executed directly via  call/1.   This
  835.  
  836. enables  the  user  to  dynamically  modify  the  environment,  e.g. via op
  837.  
  838. declarations (see Section 3.2), asserts etc.
  839.  
  840.  
  841.      A point to note is that if the environment is modified as a result  of
  842.  
  843. an execution directive, the modifications are visible only in that environ-
  844.  
  845. ment.  This means that consulted code, which runs  in  the  environment  in
  846.  
  847. which  the  source program is read in (and which is modified by such execu-
  848.  
  849. tion directives) feel the effects of such execution  directives.   However,
  850.  
  851. byte  code  resulting  from  compilation, which, in general, executes in an
  852.  
  853.  
  854.                                     11
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867. environment different from that in which the source was compiled, does  not
  868.  
  869. inherit  the  effects  of  such directives.  Thus, an op declaration can be
  870.  
  871. used in a source file to change the syntax and allow the remainder  of  the
  872.  
  873. program  to  be  parsed  according  to  the modified syntax; however, these
  874.  
  875. modifications will not, in general, manifest themselves if the byte code is
  876.  
  877. executed  in  another  environment.   Of course, if the byte code is loaded
  878.  
  879. into the same environment as that in which the source program was compiled,
  880.  
  881. e.g. through
  882.  
  883.             | ?- compile(foo, bar), load(bar).
  884.  
  885.  
  886. the effects of execution directives will continue to be felt.
  887.  
  888.  
  889. 3.  Syntax
  890.  
  891.  
  892.  
  893.  
  894. 3.1.  Terms
  895.  
  896.  
  897.  
  898.  
  899.      The syntax of SB-Prolog is by and large compatible  with  that  of  C-
  900.  
  901. Prolog.   The  data objects of the language are called  terms.  A  term  is
  902.  
  903. either a constant, a  variable  or  a  compound  term.   Constants  can  be
  904.  
  905. integers  or  atoms.   The  symbol for an atom must begin with a lower case
  906.  
  907. letter or the dollar sign $, and consist of any number of letters,  digits,
  908.  
  909. underscores  and  dollar  signs;  if  it  contains any character other than
  910.  
  911. these,  it  must  be  enclosed  within  single  quotes.[2]  As   in   other
  912. ____________________
  913.    [2] Users are advised against using symbols beginning with `$' or  `_$',
  914. however,  in  order  to  minimize the possibility of conflicts with symbols
  915. internal to the system.
  916.  
  917.  
  918.  
  919.  
  920.                                     12
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933. programming languages,  constants  are definite elementary objects.
  934.  
  935.  
  936.      Variables are distinguished by an initial capital  letter  or  by  the
  937.  
  938. initial character "_", for example
  939.  
  940.     X   Value   A   A1   _3   _RESULT   _result
  941.  
  942.  
  943. If a variable is only referred to once, it does not need to  be  named  and
  944.  
  945. may  be  written as an anonymous variable, indicated by the underline char-
  946.  
  947. acter "_".
  948.  
  949.  
  950.      A variable should be thought of as  standing  for  some  definite  but
  951.  
  952. unidentified   object.   A  variable  is  not  simply  a  writeable storage
  953.  
  954. location  as  in  most programming languages;  rather it is  a  local  name
  955.  
  956. for  some  data  object,  cf.   the  variable  of  pure  LISP  and constant
  957.  
  958. declarations in Pascal.
  959.  
  960.  
  961.      The structured data objects of the language are the compound terms.  A
  962.  
  963. compound  term  comprises  a  functor  (called the principal functor of the
  964.  
  965. term) and a sequence of one or more terms called arguments.  A  functor  is
  966.  
  967. characterised  by  its  name,  which is an atom, and its arity or number of
  968.  
  969. arguments.  For example the compound term whose functor is named `point' of
  970.  
  971. arity 3, with arguments X, Y and Z, is written
  972.  
  973.     point(X,Y,Z)
  974.  
  975.  
  976. An atom is considered to be a functor of arity 0.
  977.  
  978.  
  979.      A functor or predicate symbol is uniquely identified by its  name  and
  980.  
  981. arity  (in  other  words,  it is possible for different symbols having dif-
  982.  
  983. ferent arities to share the same name).  A functor or  predicate  symbol  p
  984.  
  985.  
  986.                                     13
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999. with arity n is usually written p/n.
  1000.  
  1001.  
  1002.      One  may  think  of  a  functor  as  a record type and  the  arguments
  1003.  
  1004. of a compound term as the  fields  of  a  record.   Compound terms are use-
  1005.  
  1006. fully pictured as trees.  For example, the term
  1007.  
  1008.     s(np(john),vp(v(likes),np(mary)))
  1009.  
  1010.  
  1011. would be pictured as the structure
  1012.  
  1013.                                      s
  1014.                                    /  \
  1015.                                 np      vp
  1016.                                 |      /  \
  1017.                               john    v    np
  1018.                                       |    |
  1019.                                    likes  mary
  1020.  
  1021.  
  1022.  
  1023.      Sometimes it is convenient to write certain functors as  operators  --
  1024.  
  1025. 2-ary  functors  may  be  declared as infix operators and 1-ary functors as
  1026.  
  1027. prefix or postfix operators.  Thus it is possible to write
  1028.  
  1029.     X+Y     (P;Q)     X<Y      +X     P;
  1030.  
  1031.  
  1032. as optional alternatives to
  1033.  
  1034.     +(X,Y)   ;(P,Q)   <(X,Y)   +(X)   ;(P)
  1035.  
  1036.  
  1037. Operators are described fully in the next section.
  1038.  
  1039.  
  1040.      Lists form an important class of data structures in Prolog.  They  are
  1041.  
  1042. essentially   the  same  as  the  lists  of LISP: a list either is the atom
  1043.  
  1044. [], representing the empty list, or  is  a  compound  term   with   functor
  1045.  
  1046. `.'/2  and   two   arguments  which  are  respectively the head and tail of
  1047.  
  1048. the list.  Thus  a  list  of  the  first  three  natural  numbers  is   the
  1049.  
  1050.  
  1051.  
  1052.                                     14
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064.  
  1065. structure
  1066.  
  1067.                                       .
  1068.                                     / \
  1069.                                   1    .
  1070.                                       / \
  1071.                                     2    .
  1072.                                        / \
  1073.                                      3   []
  1074.  
  1075.  
  1076. which could be written, using the standard syntax, as
  1077.  
  1078.     .(1,.(2,.(3,[])))
  1079.  
  1080.  
  1081. but which is normally written, in a special list notation, as [1,2,3].  The
  1082.  
  1083. special list notation in the case when the tail of  a  list  is  a variable
  1084.  
  1085. is exemplified by
  1086.  
  1087.     [X|L]     [a,b|L]
  1088.  
  1089.  
  1090. representing
  1091.  
  1092.                              .               .
  1093.                             / \             / \
  1094.                            X   L           a   .
  1095.                                               / \
  1096.                                              b   L
  1097.  
  1098.  
  1099. respectively.
  1100.  
  1101.  
  1102.      Note that this list syntax is only syntactic sugar for  terms  of  the
  1103.  
  1104. form  `.'(_,_) and does not provide any new facilities that were not avail-
  1105.  
  1106. able otherwise.
  1107.  
  1108.  
  1109.      For convenience, a further   notational   variant   is   allowed   for
  1110.  
  1111. lists   of   integers   which  correspond  to ASCII character codes.  Lists
  1112.  
  1113. written in this notation are called strings.  For example,
  1114.  
  1115.  
  1116.  
  1117.  
  1118.                                     15
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.  
  1130.     "Prolog"
  1131.  
  1132.  
  1133. represents exactly the same list as
  1134.  
  1135.     [80,114,111,108,111,103]
  1136.  
  1137.  
  1138.  
  1139. 3.2.  Operators
  1140.  
  1141.  
  1142.  
  1143.  
  1144.      Operators in Prolog are simply a notational convenience.  For example,
  1145.  
  1146. the expression
  1147.  
  1148.     2 + 1
  1149.  
  1150.  
  1151. could also be written +(2,1).  It should be noticed  that  this  expression
  1152.  
  1153. represents the structure
  1154.  
  1155.                                      +
  1156.                                    /   \
  1157.                                   2     1
  1158.  
  1159.  
  1160. and not the number 3.  The addition would only be performed if  the  struc-
  1161.  
  1162. ture  was passed as an argument to an appropriate procedure (such as eval/2
  1163.  
  1164. - see Section 5.2).
  1165.  
  1166.  
  1167.      The Prolog syntax caters for operators   of   three   main   kinds   -
  1168.  
  1169. infixprefix and postfix.  An infix operator appears between its two argu-
  1170.  
  1171. ments, while a prefix operator precedes its single argument and  a  postfix
  1172.  
  1173. operator is written after its single argument.
  1174.  
  1175.  
  1176.      Each operator has a precedence, which is a number from  1   to   1200.
  1177.  
  1178. The  precedence  is  used  to  disambiguate expressions  where  the  struc-
  1179.  
  1180. ture  of  the  term  denoted is not made explicit through parenthesization.
  1181.  
  1182.  
  1183.  
  1184.                                     16
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197. The   general  rule is that the operator with the highest precedence is the
  1198.  
  1199. principal functor.  Thus if `+'  has  a higher precedence  than  `/',  then
  1200.  
  1201. ``a+b/c'' and ``a+(b/c)'' are equivalent and denote the term "+(a,/(b,c))".
  1202.  
  1203. Note that the  infix form of the term "/(+(a,b),c)" must  be  written  with
  1204.  
  1205. explicit parentheses, ``(a+b)/c''.
  1206.  
  1207.  
  1208.      If there are two operators in  the  subexpression  having   the   same
  1209.  
  1210. highest   precedence,  the ambiguity must be resolved from the types of the
  1211.  
  1212. operators.  The possible types for an infix operator are
  1213.  
  1214.     xfx     xfy     yfx
  1215.  
  1216.  
  1217. With an operator of type `xfx', it is a requirement that both  of  the  two
  1218.  
  1219. subexpressions  which  are  the  arguments of the operator must be of lower
  1220.  
  1221. precedence  than  the  operator  itself,  i.e.   their  principal  functors
  1222.  
  1223. must   be   of   lower   precedence, unless the subexpression is explicitly
  1224.  
  1225. bracketed  (which  gives  it  zero  precedence).  With  an operator of type
  1226.  
  1227. `xfy',  only  the  first  or  left-hand subexpression must be of lower pre-
  1228.  
  1229. cedence;  the second can be of the same  precedence  as the main  operator;
  1230.  
  1231. and vice versa for an operator of type `yfx'.
  1232.  
  1233.  
  1234.      For example, if the operators `+' and `-' both  have  type  `yfx'  and
  1235.  
  1236. are  of  the  same  precedence, then the expression ``a-b+c'' is valid, and
  1237.  
  1238. means ``(a-b)+c'', i.e. ``+(-(a,b),c)''.  Note that the expression would be
  1239.  
  1240. invalid  if  the   operators   had  type `xfx', and would mean ``a-(b+c)'',
  1241.  
  1242. i.e. ``-(a,+(b,c))'', if the types were both `xfy'.
  1243.  
  1244.  
  1245.      The possible types for a prefix operator are
  1246.  
  1247.  
  1248.  
  1249.  
  1250.                                     17
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.  
  1262.     fx        fy
  1263.  
  1264.  
  1265. and for a postfix operator they are
  1266.  
  1267.     xf        yf
  1268.  
  1269.  
  1270. The meaning of the types should be clear  by   analogy   with   those   for
  1271.  
  1272. infix   operators.    As   an  example,  if `not' were declared as a prefix
  1273.  
  1274. operator of type `fy', then
  1275.  
  1276.     not not P
  1277.  
  1278.  
  1279. would be a permissible way to write  "not(not(P))".  If   the   type   were
  1280.  
  1281. `fx', the preceding expression would not be legal, although
  1282.  
  1283.     not P
  1284.  
  1285.  
  1286. would still be a permissible form for "not(P)".
  1287.  
  1288.  
  1289.      In SB-Prolog, a functor named name is  declared  as   an  operator  of
  1290.  
  1291. type type and precedence precedence by calling the evaluable predicate op:
  1292.  
  1293.             | ?- op(precedence,type,name).
  1294.  
  1295.  
  1296. The argument name can also be a list of names of operators of the same type
  1297.  
  1298. and precedence.
  1299.  
  1300.  
  1301.      It is possible to have more than one operator of the  same   name,  so
  1302.  
  1303. long  as  they  are of different kinds, i.e.  infix, prefix or postfix.  An
  1304.  
  1305. operator of any kind may be redefined by a new declaration   of   the  same
  1306.  
  1307. kind.    This   applies equally to operators which are provided as standard
  1308.  
  1309. in SB-Prolog, namely:
  1310.  
  1311.     :- op(   1200,   xfx,   [ :-, --> ]).
  1312.     :- op(   1200,    fx,   [ :-, ?- ]).
  1313.     :- op(   1198,   xfx,   [ ::- ]).
  1314.  
  1315.  
  1316.                                     18
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.     :- op(   1100,   xfy,   [ ; ]).
  1323.     :- op(   1050,   xfy,   [ -> ]).
  1324.     :- op(   1000,   xfy,   [ ',' ]).   /* See note below */
  1325.     :- op(    900,    fy,   [ not, \+, spy, nospy ]).
  1326.     :- op(    700,   xfx,   [ =, is, =.., ==, \==, @<, @>, @=<, @>=,
  1327.                               =:=, =\=, <, >, =<, >= ]).
  1328.     :- op(    661,   xfy,   [ `.' ]).
  1329.     :- op(    500,   yfx,   [ +, -, /\, \/ ]).
  1330.     :- op(    500,    fx,   [ +, - ]).
  1331.     :- op(    400,   yfx,   [ *, /, //, <<, >> ]).
  1332.     :- op(    300,   xfx,   [ mod ]).
  1333.     :- op(    200,   xfy,   [ ^ ]).
  1334.  
  1335.  
  1336.  
  1337.  
  1338.      Operator declarations are most usefully placed in  directives  at  the
  1339.  
  1340. top  of  your program files. In this case the directive should be a command
  1341.  
  1342. as shown above. Another common method of organisation is to have  one  file
  1343.  
  1344. just  containing commands to declare all the necessary operators. This file
  1345.  
  1346. is then always consulted first.
  1347.  
  1348.  
  1349.      Note that a comma written literally as  a  punctuation  character  can
  1350.  
  1351. be  used  as  though  it were an infix operator of precedence 1000 and type
  1352.  
  1353. `xfy':
  1354.  
  1355.     X,Y    ','(X,Y)
  1356.  
  1357.  
  1358. represent the same compound term.  But note that  a  comma  written  as   a
  1359.  
  1360. quoted atom is not a standard operator.
  1361.  
  1362.  
  1363.      Note also that the  arguments  of   a   compound   term   written   in
  1364.  
  1365. standard   syntax  must be expressions of precedence below 1000. Thus it is
  1366.  
  1367. necessary to parenthesize the expression "P :- Q" in
  1368.  
  1369.     assert((P :- Q))
  1370.  
  1371.  
  1372. The following syntax restrictions  serve   to  remove  potential  ambiguity
  1373.  
  1374. associated with prefix operators.
  1375.  
  1376.  
  1377. -    In a term written in standard syntax, the  principal  functor  and its
  1378.  
  1379.      following  "("  must  not be separated by any whitespace.  Thus
  1380.  
  1381.  
  1382.                                     19
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.          point (X,Y,Z)
  1395.  
  1396.  
  1397.      is invalid syntax (unless point were declared as a prefix operator).
  1398.  
  1399.  
  1400. -    If the argument of a prefix operator starts with  a  "(",   this   "("
  1401.  
  1402.      must  be  separated  from  the operator by at least one space or other
  1403.  
  1404.      non-printable character.  Thus
  1405.  
  1406.          :-(p;q),r.
  1407.  
  1408.  
  1409.      (where `:-' is the prefix operator) is invalid  syntax,  and  must  be
  1410.  
  1411.      written as
  1412.  
  1413.          :- (p;q),r.
  1414.  
  1415.  
  1416.  
  1417. -    If a prefix  operator  is  written  without   an   argument,   as   an
  1418.  
  1419.      ordinary  atom,  the  atom  is  treated  as  an expression of the same
  1420.  
  1421.      precedence as the prefix operator, and  must  therefore  be  bracketed
  1422.  
  1423.      where necessary.  Thus the brackets are necessary in
  1424.  
  1425.          X = (?-)
  1426.  
  1427.  
  1428.  
  1429. 4.  SB-PrologOperational Semantics
  1430.  
  1431.  
  1432.  
  1433.  
  1434. 4.1.  Standard Execution Behaviour
  1435.  
  1436.  
  1437.  
  1438.  
  1439.      The normal execution behaviour of SB-Prolog follows the usual left  to
  1440.  
  1441. right  order  of  literals  within  a clause, and the textual top to bottom
  1442.  
  1443. order of clauses for a predicate.  This corresponds to a depth first search
  1444.  
  1445. of  the leftmost SLD-tree for the program and the given query.  Unification
  1446.  
  1447.  
  1448.                                     20
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460.  
  1461. without occurs check is used, and execution backtracks to the  most  recent
  1462.  
  1463. choice point when unification fails.
  1464.  
  1465.  
  1466. 4.2.  Cuts and If-Then-Else
  1467.  
  1468.  
  1469.  
  1470.  
  1471.      This standard execution behaviour of SB-Prolog can  be  changed  using
  1472.  
  1473. constructs  like cut (`!') and if-then-else (`->').  In SB-Prolog, cuts are
  1474.  
  1475. usually treated as hard, i.e. discard choice points of all the literals  to
  1476.  
  1477. the  left  of  the cut in the clause containing the cut being executed, and
  1478.  
  1479. also the choice point for the parent predicate, i.e.  any remaining clauses
  1480.  
  1481. for the predicate containing the cut being executed.  There are some situa-
  1482.  
  1483. tions, however, where the scope of a cut is restricted to be  smaller  than
  1484.  
  1485. this.  Restrictions apply under the following conditions:
  1486.  
  1487.  
  1488. (1)  The cut occurs in a term which has been  constructed  at  runtime  and
  1489.  
  1490.      called through call/1, e.g. in
  1491.  
  1492.                    ..., X = (p(Y), !, q(Y)), ..., call(X), ...
  1493.  
  1494.  
  1495.      In this case, the scope of the cut is  restricted  to  be  within  the
  1496.  
  1497.      call,  unless  one of the following cases also apply and serve to res-
  1498.  
  1499.      trict its scope further.
  1500.  
  1501.  
  1502. (2)  The cut occurs in a negated goal, or within the scope of  an  if-then-
  1503.  
  1504.      else.  In these cases, the scope of the cut is restricted to be within
  1505.  
  1506.      the negation or the if-then-else.
  1507.  
  1508.  
  1509. In cases involving nested occurrences of these situations, the scope of the
  1510.  
  1511. cut  is restricted to that for the deepest such nested construct, i.e. most
  1512.  
  1513.  
  1514.                                     21
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524.  
  1525.  
  1526.  
  1527. restricted.  For example, in the construct
  1528.  
  1529.  ..., not( (p(X) -> not( (q(X), (r(X) -> s(X) ; (t(X), !, u(X)))) )) ), ...
  1530.  
  1531.  
  1532. the scope of the cut is restricted to the inner if-then-else, and does  not
  1533.  
  1534. affect any choice point that may have been set up for q(X).
  1535.  
  1536.  
  1537.      The behaviour of the if-then-else operator `->' is  as  follows:  when
  1538.  
  1539. executing the goal
  1540.  
  1541.                                 P -> Q ; R
  1542.  
  1543.  
  1544. if P succeeds, then any alternatives for P are discarded and  Q  is  tried,
  1545.  
  1546. while if P fails then R is tried.  Thus, -> may be thought of as
  1547.  
  1548.  
  1549.  
  1550.             P -> Q ; R :- P, !, Q.
  1551.             P -> Q ; R :- R.
  1552.  
  1553.  
  1554.  
  1555.  
  1556. The scoping of cuts within if-then-elses is consistent with this  conceptu-
  1557.  
  1558. alization.
  1559.  
  1560.  
  1561. 4.3.  Unification of Floating Point Numbers
  1562.  
  1563.  
  1564.  
  1565.  
  1566.      As far as unification  is  concerned,  no  type  distinction  is  made
  1567.  
  1568. between  integers  and floating point numbers, and no explicit type conver-
  1569.  
  1570. sion is necessary when unifying an integer with a float.  However,  due  to
  1571.  
  1572. the  finite  precision representation of floating point numbers and cumula-
  1573.  
  1574. tive round-off errors in floating point arithmetic,  comparisons  involving
  1575.  
  1576. floating point numbers may not always give the expected results.  An effort
  1577.  
  1578.  
  1579.  
  1580.                                     22
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589.  
  1590.  
  1591.  
  1592.  
  1593. is made to minimize surprises by considering two numbers x and y (at  least
  1594.  
  1595. one of which is a float) to be unifiable if ||x| - |y||/min(|x|, |y|) to be
  1596.  
  1597. less than 10[-5].   However,  this  does  not  guarantee  immunity  against
  1598.  
  1599. round-off  errors.   For the same reason, users are warned that indexing on
  1600.  
  1601. predicate arguments (see Section 13) may not give the expected  results  if
  1602.  
  1603. floating point numbers are involved.
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.                                     23
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659. 5.  Evaluable Predicates
  1660.  
  1661.  
  1662.  
  1663.  
  1664.      This section describes (most of) the evaluable predicates provided  by
  1665.  
  1666. SB-Prolog.   These  can  be  divided into three classes: inline predicates,
  1667.  
  1668. builtin predicates and library predicates.
  1669.  
  1670.  
  1671.      Inline predicates  represent  ``primitive''  operations  in  the  WAM.
  1672.  
  1673. Calls to inline predicates are compiled into a sequence of WAM instructions
  1674.  
  1675. in-line, i.e. without actually making a call to the predicate.   Thus,  for
  1676.  
  1677. example, relational predicates (>/2, >=/2, etc.) compile to, essentially, a
  1678.  
  1679. subtraction and a conditional branch.  Inline predicates  cannot  be  rede-
  1680.  
  1681. fined by the user.  Table 1 lists the SB-Prolog inline predicates.
  1682.  
  1683.  
  1684.      Unlike inline predicates, builtin  predicates  are  implemented  by  C
  1685.  
  1686. functions  in the simulator, and accessed via the inline predicate `_$buil-
  1687.  
  1688. tin'/1.  Thus, if a builtin predicate foo/3 was defined as  builtin  number
  1689.  
  1690. 38, there would be a definition in the system of the form
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696. =/2                 </2                  =</2                 >=/2
  1697. >/2                 /\/2                 `\/'/2               <</2
  1698. >>/2                =:=/2                =\=/2                is/2
  1699. /1                  `_$savecp'/1         `_$cutto'/1          `_$builtin'/1
  1700. `_$call'/1          nonvar/1             var/1                fail/0
  1701. halt/0              true/0
  1702.  
  1703.  
  1704.                   Table 1: Inline Predicates of SB-Prolog
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.                                     24
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721.  
  1722.  
  1723.  
  1724.             foo(X,Y,Z) :- '_$builtin'(38).
  1725.  
  1726.  
  1727.  
  1728.  
  1729.  
  1730.      In effect, a builtin is simply a segment of code in a large case (i.e.
  1731.  
  1732. switch)  statement.   Each  builtin is identified internally by an integer,
  1733.  
  1734. referred to as its ``builtin number'', associated with it.  The code for  a
  1735.  
  1736. builtin  with builtin number k corresponds to the k[th.] case in the switch
  1737.  
  1738. statement.  SB-Prolog limits the total number of builtins to 256.
  1739.  
  1740.  
  1741.      Builtins, unlike inline predicates,  can be  redefined  by  the  user.
  1742.  
  1743. For example, the predicate foo/3 above can be redefined simply by compiling
  1744.  
  1745. the new definition into a directory such that during dynamic  loading,  the
  1746.  
  1747. new definition would be encountered first and loaded.
  1748.  
  1749.  
  1750.      A list of the builtins currently provided is  listed  in  Appendix  1.
  1751.  
  1752. Section  7.4  describes the procedure to be followed in order to define new
  1753.  
  1754. builtin predicates.
  1755.  
  1756.  
  1757.      Like builtin predicates, library predicates may also be  redefined  by
  1758.  
  1759. the  user.  The essential difference between builtin and library predicates
  1760.  
  1761. is that whereas the former are coded into the simulator in  C,  the  latter
  1762.  
  1763. are written in Prolog.
  1764.  
  1765.  
  1766. 5.1.  Input and Output
  1767.  
  1768.  
  1769.  
  1770.  
  1771.      Input and output are done with respect to the current input and output
  1772.  
  1773. streams.  These can be set, reset or checked using the file handling predi-
  1774.  
  1775. cates described below.  The default input and output streams are denoted by
  1776.  
  1777.  
  1778.                                     25
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.  
  1787.  
  1788.  
  1789.  
  1790.  
  1791. user, and refer to the user's terminal.
  1792.  
  1793.  
  1794. 5.1.1.  File Handling
  1795.  
  1796.  
  1797.  
  1798.  
  1799. see(F)
  1800.  
  1801.      F becomes the current input stream.  F must be instantiated to an atom
  1802.  
  1803.      at the time of the call.
  1804.  
  1805.  
  1806. seeing(F)
  1807.  
  1808.      F is unified with the name of the current input file.
  1809.  
  1810.  
  1811. seen Closes the current input stream.
  1812.  
  1813.  
  1814. tell(F)
  1815.  
  1816.      F becomes the current output stream.  F must  be  instantiated  to  an
  1817.  
  1818.      atom at the time of the call.
  1819.  
  1820.  
  1821. telling(F)
  1822.  
  1823.      F is unified with the name of the current output file.
  1824.  
  1825.  
  1826. told
  1827.  
  1828.  
  1829.      Closes the current output stream.
  1830.  
  1831.  
  1832. $exists(F)
  1833.  
  1834.      Succeeds if file F exists.
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.                                     26
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855.  
  1856.  
  1857. 5.1.2.  Term I/O
  1858.  
  1859.  
  1860.  
  1861.  
  1862. read(X)
  1863.  
  1864.      The next term, delimited by a full stop (i.e.  a "."  followed   by  a
  1865.  
  1866.      carriage-return  or   a   space),   is   read   from the current input
  1867.  
  1868.      stream and unified with X. The syntax of the term  must  accord   with
  1869.  
  1870.      current  operator  declarations.   If a call read(X) causes the end of
  1871.  
  1872.      the current input stream to be reached, X is unified  with  the   atom
  1873.  
  1874.      `end_of_file'.   Further  calls  to read for the same stream will then
  1875.  
  1876.      cause an error failure.
  1877.  
  1878.  
  1879. write(X)
  1880.  
  1881.      The term X is written to the  current  output  stream   according   to
  1882.  
  1883.      operator declarations in force.
  1884.  
  1885.  
  1886. display(X)
  1887.  
  1888.      The term X is displayed on the terminal in standard parenthesised pre-
  1889.  
  1890.      fix notation.
  1891.  
  1892.  
  1893. writeq(Term)
  1894.  
  1895.      Similar to write(Term), but the names of atoms and functors are quoted
  1896.  
  1897.      where necessary to make the result acceptable as input to read.
  1898.  
  1899.  
  1900. print(Term)
  1901.  
  1902.      Prints out the term Term onto the user's terminal.
  1903.  
  1904.  
  1905. writename(Term)
  1906.  
  1907.      If Term is an uninstantiated variable, its name,  which  looks  a  lot
  1908.  
  1909.  
  1910.                                     27
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921.  
  1922.  
  1923.      like  an  address  in memory, is written out; otherwise, the principal
  1924.  
  1925.      functor of Term is written out.
  1926.  
  1927.  
  1928. writeqname(Term)
  1929.  
  1930.      As for writename, but the names are quoted where necessary.
  1931.  
  1932.  
  1933. print_al(NA)
  1934.  
  1935.  
  1936.      Prints A (which must be an atom or a number) left-aligned in  a  field
  1937.  
  1938.      of  width  N,  with  blanks padded to the right.  If A's print name is
  1939.  
  1940.      longer than the field width N, then A is printed  but  with  no  right
  1941.  
  1942.      padding.
  1943.  
  1944.  
  1945. print_ar(NA)
  1946.  
  1947.  
  1948.      Prints A (which must be an atom or a number) right-aligned in a  field
  1949.  
  1950.      of  width  N,  with  blanks  padded to the left.  If A's print name is
  1951.  
  1952.      longer than the field width N, then A is printed but with no left pad-
  1953.  
  1954.      ding.
  1955.  
  1956.  
  1957. 5.1.3.  Character I/O
  1958.  
  1959.  
  1960.  
  1961.  
  1962. nl   A new line is started on the current output stream.
  1963.  
  1964.  
  1965. get0(N)
  1966.  
  1967.      N is the ASCII code of the next  character  from  the  current   input
  1968.  
  1969.      stream.  If  the current input stream reaches its end of file, a -1 is
  1970.  
  1971.      returned (however, unlike in C-Prolog, the input stream is not  closed
  1972.  
  1973.      on encountering end-of-file).
  1974.  
  1975.  
  1976.                                     28
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989. get(N)
  1990.  
  1991.      N is the ASCII code of the  next  non-blank  printable  character from
  1992.  
  1993.      the  current input stream. It has the same behaviour as get0 on end of
  1994.  
  1995.      file.
  1996.  
  1997.  
  1998. put(N)
  1999.  
  2000.      ASCII character code N is output to the current output stream.  N must
  2001.  
  2002.      be an integer.
  2003.  
  2004.  
  2005. tab(N)
  2006.  
  2007.      N spaces are output to the  current  output  stream.   N  must  be  an
  2008.  
  2009.      integer.
  2010.  
  2011.  
  2012. 5.2.  Arithmetic
  2013.  
  2014.  
  2015.  
  2016.  
  2017.      Arithmetic is performed by   evaluable  predicates   which   take   as
  2018.  
  2019. arguments    arithmetic  expressions   and  evaluate  them.  An  arithmetic
  2020.  
  2021. expression is a term  built  from  evaluable functors, numbers   and  vari-
  2022.  
  2023. ables.   At   the   time   of  evaluation,  each  variable in an arithmetic
  2024.  
  2025. expression must be bound to a number or  to   an   arithmetic   expression.
  2026.  
  2027. Each evaluable functor stands for an arithmetic  operation.
  2028.  
  2029.  
  2030.      The evaluable  functors  are  as  follows,   where   X   and   Y   are
  2031.  
  2032. arithmetic expressions.
  2033.  
  2034.  
  2035. X + Y
  2036.  
  2037.      addition.
  2038.  
  2039.  
  2040.  
  2041.  
  2042.                                     29
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055. X - Y
  2056.  
  2057.      subtraction.
  2058.  
  2059.  
  2060. X * Y
  2061.  
  2062.      multiplication.
  2063.  
  2064.  
  2065. X / Y
  2066.  
  2067.      division.[3]  Amounts  to  integer  division  if  both  X  and  Y  are
  2068.  
  2069.      integers.
  2070.  
  2071.  
  2072. X mod Y
  2073.  
  2074.      X (integer) modulo Y.
  2075.  
  2076.  
  2077. -X   unary minus.
  2078.  
  2079.  
  2080. X /\ Y
  2081.  
  2082.      integer bitwise conjunction.
  2083.  
  2084.  
  2085. X \/ Y
  2086.  
  2087.      integer bitwise disjunction.
  2088.  
  2089.  
  2090. X << Y
  2091.  
  2092.      integer bitwise left shift of X by Y places.
  2093.  
  2094.  
  2095. X >> Y
  2096.  
  2097.      integer bitwise right shift of X by Y places.
  2098.  
  2099.  
  2100. ____________________
  2101.    [3] The ``integer division'' operator `//' of C-Prolog is not supported;
  2102. it can be faked using floor/2 if necessary.
  2103.  
  2104.  
  2105.  
  2106.  
  2107.  
  2108.                                     30
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.  
  2117.  
  2118.  
  2119.  
  2120.  
  2121. \X   integer bitwise negation.
  2122.  
  2123.  
  2124.      As far as unification  is  concerned,  no  type  distinction  is  made
  2125.  
  2126. between  integers  and floating point numbers, and no explicit type conver-
  2127.  
  2128. sion is necessary when unifying an integer with a float.  However,  due  to
  2129.  
  2130. the  finite  precision representation of floating point numbers and cumula-
  2131.  
  2132. tive round-off errors in floating point arithmetic,  comparisons  involving
  2133.  
  2134. floating point numbers may not always give the expected results.  An effort
  2135.  
  2136. is made to minimize surprises by considering two numbers x and y (at  least
  2137.  
  2138. one  of  which  is  a float) to be unifiable if |x - y|/min(|x|, |y|) to be
  2139.  
  2140. less than 10[-5].  The user  should  note,  however,  that  this  does  not
  2141.  
  2142. guarantee immunity against round-off errors.
  2143.  
  2144.  
  2145.      The arithmetic evaluable predicates are as  follows,  where  X  and  Y
  2146.  
  2147. stand  for  arithmetic  expressions,  and  Z for some term.  Note that this
  2148.  
  2149. means that is only evaluates one of its arguments as an arithmetic  expres-
  2150.  
  2151. sion  (the  right-hand  side  one),  whereas  all the comparison predicates
  2152.  
  2153. evaluate both their arguments.
  2154.  
  2155.  
  2156. Z is X
  2157.  
  2158.  
  2159.      Arithmetic expression X is evaluated and the result, is  unified  with
  2160.  
  2161.      Z.  Fails  if X is not an arithmetic expression.  One point to note is
  2162.  
  2163.      that in compiled code, the current implementation of is/2 cannot  han-
  2164.  
  2165.      dle compound expressions created dynamically: these have to be handled
  2166.  
  2167.      using eval/2.  Thus, for example, the goals
  2168.  
  2169.                     ..., X = Y + Z, Y = 3, Z = 2, W is X, ...
  2170.  
  2171.  
  2172.  
  2173.  
  2174.                                     31
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186.  
  2187.      would fail, whereas
  2188.  
  2189.                   ..., X = Y + Z, Y = 3, Z = 2, eval(X,W), ...
  2190.  
  2191.  
  2192.      could succeed.
  2193.  
  2194.  
  2195. eval(EX)
  2196.  
  2197.      Evaluates the arithmetic expression E and unifies the result with  the
  2198.  
  2199.      term  X.   Fails  if E is not an arithmetic expression.  The principal
  2200.  
  2201.      difference between eval/2 and is/2 is that in compiled code, is/2 can-
  2202.  
  2203.      not  handle arithmetic expressions that are compound terms constructed
  2204.  
  2205.      at runtime, but eval/2 can.  On the other hand, is/2 is more efficient
  2206.  
  2207.      for  the  evaluation  of static arithmetic expressions (i.  e. expres-
  2208.  
  2209.      sions that do not have compound subterms created at runtime).  Another
  2210.  
  2211.      difference  is  that while is/2 is an inline predicate, eval/2 is not;
  2212.  
  2213.      thus, is/2 cannot be redefined by the user, but eval/2 can.
  2214.  
  2215.  
  2216. X =:= Y
  2217.  
  2218.  
  2219.      The values of X and Y are equal.  If either X or  Y  involve  compound
  2220.  
  2221.      subexpressions  that  are  created  at  runtime,  they should first be
  2222.  
  2223.      evaluated using eval/2.
  2224.  
  2225.  
  2226. X =\= Y
  2227.  
  2228.  
  2229.      The values of X and Y are not equal.  If either X or  Y  involve  com-
  2230.  
  2231.      pound subexpressions that are created at runtime, they should first be
  2232.  
  2233.      evaluated using eval/2.
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239.  
  2240.                                     32
  2241.  
  2242.  
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248.  
  2249.  
  2250.  
  2251.  
  2252.  
  2253. X < Y
  2254.  
  2255.  
  2256.      The value of X is less than the value of Y.  If either X or Y  involve
  2257.  
  2258.      compound subexpressions that are created at runtime, they should first
  2259.  
  2260.      be evaluated using eval/2.
  2261.  
  2262.  
  2263. X > Y
  2264.  
  2265.  
  2266.      The value of X is greater than the value of  Y.   If  either  X  or  Y
  2267.  
  2268.      involve  compound  subexpressions  that  are  created at runtime, they
  2269.  
  2270.      should first be evaluated using eval/2.
  2271.  
  2272.  
  2273. X =< Y
  2274.  
  2275.  
  2276.      The value of X is less than or equal to the value of Y.  If  either  X
  2277.  
  2278.      or Y involve compound subexpressions that are created at runtime, they
  2279.  
  2280.      should first be evaluated using eval/2.
  2281.  
  2282.  
  2283. X >= Y
  2284.  
  2285.  
  2286.      The value of X is greater than or equal to the value of Y.  If  either
  2287.  
  2288.      X  or  Y  involve compound subexpressions that are created at runtime,
  2289.  
  2290.      they should first be evaluated using eval/2.
  2291.  
  2292.  
  2293. floor(XY)
  2294.  
  2295.  
  2296.      If X is a floating point number in the call and Y is free, then  Y  is
  2297.  
  2298.      instantiated  to  the  largest  integer  whose  absolute  value is not
  2299.  
  2300.      greater than the absolute value of X; if X is  uninstantiated  in  the
  2301.  
  2302.      call and Y is an integer, then X is instantiated to the smallest float
  2303.  
  2304.  
  2305.  
  2306.                                     33
  2307.  
  2308.  
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314.  
  2315.  
  2316.  
  2317.  
  2318.  
  2319.      not less than Y.
  2320.  
  2321.  
  2322. floatc(FME)
  2323.  
  2324.  
  2325.      If F is a number while M and E are uninstantiated in the call, then  M
  2326.  
  2327.      is  instantiated  to a float m (of magnitude less than 1), and E to an
  2328.  
  2329.      integer n, such that
  2330.  
  2331.                                  F = m * 2[n].
  2332.  
  2333.  
  2334.      If F is uninstantiated in the call  while  M  is  a  float  and  E  an
  2335.  
  2336.      integer, then F becomes instantiated to M * 2[E].
  2337.  
  2338.  
  2339. exp(XY)
  2340.  
  2341.  
  2342.      If X is instantiated to a number and Y is uninstantiated in the  call,
  2343.  
  2344.      then  Y  is instantiated to e[X] (where e = 2.71828...); if X is unin-
  2345.  
  2346.      stantiated in the call while Y is instantiated to a  positive  number,
  2347.  
  2348.      then X is instantiated to log<e>(Y).
  2349.  
  2350.  
  2351. square(XY)
  2352.  
  2353.  
  2354.      If X is instantiated to a number while  Y  is  uninstantiated  in  the
  2355.  
  2356.      call,  then  Y becomes instantiated to X[2]; if X is uninstantiated in
  2357.  
  2358.      the call while Y is instantiated to a positive number, then X  becomes
  2359.  
  2360.      instantiated to the positive square root of Y (if Y is negative in the
  2361.  
  2362.      call, X becomes instantiated to 0.0).
  2363.  
  2364.  
  2365. sin(XY)
  2366.  
  2367.  
  2368.  
  2369.  
  2370.  
  2371.  
  2372.                                     34
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.  
  2381.  
  2382.  
  2383.  
  2384.  
  2385.      If X is instantiated to a number (representing an  angle  in  radians)
  2386.  
  2387.      and  Y  is  uninstantiated in the call, then Y becomes instantiated to
  2388.  
  2389.      sin(X) (the user should check the magnitude of X to make sure that the
  2390.  
  2391.      result is meaningful).  If Y is instantiated to a number between -~~/2
  2392.  
  2393.      and ~~/2 and X is uninstantiated in the call, then X  becomes  instan-
  2394.  
  2395.      tiated to sin[-1](Y).
  2396.  
  2397.  
  2398. 5.3.  Convenience
  2399.  
  2400.  
  2401.  
  2402.  
  2403. P , Q
  2404.  
  2405.      P and then Q.
  2406.  
  2407.  
  2408. P ; Q
  2409.  
  2410.      P or Q.
  2411.  
  2412.  
  2413.      Always  succeeds.
  2414.  
  2415.  
  2416. X = Y
  2417.  
  2418.      Defined as if by the clause " Z=Z ", i.e. X and Y are unified.
  2419.  
  2420.  
  2421. 5.4.  Extra Control
  2422.  
  2423.  
  2424.  
  2425.  
  2426. !    Cut (discard) all choice points made since  the  parent  goal  started
  2427.  
  2428.      execution.   (The  scope  of cut in different contexts is discussed in
  2429.  
  2430.      Section 4.2).
  2431.  
  2432.  
  2433. not P
  2434.  
  2435.      If the goal P has a solution,  fail,   otherwise   succeed.    It   is
  2436.  
  2437.  
  2438.                                     35
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.  
  2448.  
  2449.  
  2450.  
  2451.      defined as if by
  2452.  
  2453.          not(P) :- P, !, fail.
  2454.          not(_).
  2455.  
  2456.  
  2457.  
  2458. P -> Q ; R
  2459.  
  2460.      Analogous to "if P then Q else R" i.e.  defined as if by
  2461.  
  2462.          P -> Q ; R :- P, !, Q.
  2463.          P -> Q ; R :- R.
  2464.  
  2465.  
  2466.  
  2467. P -> Q
  2468.  
  2469.      When occurring other  than  as  one  of  the  alternatives  of  a dis-
  2470.  
  2471.      junction, is equivalent to
  2472.  
  2473.          P -> Q ; fail.
  2474.  
  2475.  
  2476.  
  2477. repeat
  2478.  
  2479.      Generates an  infinite  sequence  of  backtracking  choices.    It  is
  2480.  
  2481.      defined by the clauses:
  2482.  
  2483.          repeat.
  2484.          repeat :- repeat.
  2485.  
  2486.  
  2487.  
  2488. fail Always fails.
  2489.  
  2490.  
  2491. 5.5.  Meta-Logical
  2492.  
  2493.  
  2494.  
  2495.  
  2496. var(X)
  2497.  
  2498.      Tests whether X is currently instantiated to a variable.
  2499.  
  2500.  
  2501.  
  2502.  
  2503.  
  2504.                                     36
  2505.  
  2506.  
  2507.  
  2508.  
  2509.  
  2510.  
  2511.  
  2512.  
  2513.  
  2514.  
  2515.  
  2516.  
  2517. nonvar(X)
  2518.  
  2519.      Tests whether X is currently instantiated to a non-variable term.
  2520.  
  2521.  
  2522. atom(X)
  2523.  
  2524.      Checks that X is  currently  instantiated  to   an   atom   (i.e.    a
  2525.  
  2526.      non-variable term of arity 0, other than a number).
  2527.  
  2528.  
  2529. integer(X)
  2530.  
  2531.      Checks that X is currently instantiated to an integer.
  2532.  
  2533.  
  2534. real(X)
  2535.  
  2536.  
  2537.      Checks that X is currently instantiated to a floating point number..
  2538.  
  2539.  
  2540. number(X)
  2541.  
  2542.      Checks that X is currently instantiated to a number, i.e. that  it  is
  2543.  
  2544.      either an integer or a real.
  2545.  
  2546.  
  2547. atomic(X)
  2548.  
  2549.      Checks that X is currently instantiated to an atom or number.
  2550.  
  2551.  
  2552. structure(X)
  2553.  
  2554.  
  2555.      Checks that X is currently instantiated to a compound term, i.e. to  a
  2556.  
  2557.      nonvariable term that is not atomic.
  2558.  
  2559.  
  2560. functor(T,F,N)
  2561.  
  2562.      The principal functor of term T has name F and arity N,  where   F  is
  2563.  
  2564.      either   an   atom  or, provided N is 0, a number. Initially, either T
  2565.  
  2566.      must  be  instantiated  to  a  non-variable,  or  F  and  N   must  be
  2567.  
  2568.  
  2569.  
  2570.                                     37
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578.  
  2579.  
  2580.  
  2581.  
  2582.  
  2583.      instantiated    to,    respectively,   either   an   atom  and  a non-
  2584.  
  2585.      negative integer or an integer and 0.  If  these  conditions  are  not
  2586.  
  2587.      satisfied,  an  error  message  is given.  In the case where T is ini-
  2588.  
  2589.      tially instantiated to a variable, the result  of  the   call   is  to
  2590.  
  2591.      instantiate   T  to the most general term having the principal functor
  2592.  
  2593.      indicated.
  2594.  
  2595.  
  2596. arg(I,T,X)
  2597.  
  2598.      Initially, I must be instantiated to a positive integer and  T   to  a
  2599.  
  2600.      compound   term.   The  result  of the call is to unify X with the Ith
  2601.  
  2602.      argument of term  T.  (The  arguments  are  numbered from  1 upwards.)
  2603.  
  2604.      If  the initial conditions are not satisfied or I is out of range, the
  2605.  
  2606.      call merely fails.
  2607.  
  2608.  
  2609. X =.. Y
  2610.  
  2611.      Y is a list whose head is the  atom  corresponding  to  the  principal
  2612.  
  2613.      functor   of  X and whose tail is the argument list of that functor in
  2614.  
  2615.      X. E.g.
  2616.  
  2617.          product(0,N,N-1) =.. [product,0,N,N-1]
  2618.  
  2619.          N-1 =.. [-,N,1]
  2620.  
  2621.          product =.. [product]
  2622.  
  2623.  
  2624.      If X is instantiated to a variable, then Y must be instantiated either
  2625.  
  2626.      to a list of determinate length whose head is an atom, or to a list of
  2627.  
  2628.      length 1 whose head is a number.
  2629.  
  2630.  
  2631. name(X,L)
  2632.  
  2633.      If X is an atom or a number then L is a list of the ASCII codes of the
  2634.  
  2635.  
  2636.                                     38
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644.  
  2645.  
  2646.  
  2647.  
  2648.  
  2649.      characters comprising the name of X. E.g.
  2650.  
  2651.          name(product,[112,114,111,100,117,99,116])
  2652.  
  2653.  
  2654.      i.e.  name(product,"product").
  2655.  
  2656.  
  2657. If X is instantiated to a variable, L must be instantiated  to  a  list  of
  2658.  
  2659. ASCII character codes.  E.g.
  2660.  
  2661.     | ?- name(X,[104,101,108,108,111])).
  2662.  
  2663.     X = hello
  2664.  
  2665.     | ?- name(X,"hello").
  2666.  
  2667.     X = hello
  2668.  
  2669.  
  2670.  
  2671. call(X)
  2672.  
  2673.      If X is a nonvariable term in the program text, then  it  is  executed
  2674.  
  2675.      exactly as if X appeared in the program text instead of call(X), e.g.
  2676.  
  2677.           ..., p(a), call( (q(X), r(Y)) ), s(X), ...
  2678.  
  2679.  
  2680.      is equivalent to
  2681.  
  2682.           ..., p(a), q(X), r(Y), s(X), ...
  2683.  
  2684.  
  2685.      However, if X is a variable in the program text, then if at runtime  X
  2686.  
  2687.      is  instantiated  to a term which would be acceptable as the body of a
  2688.  
  2689.      clause, the goal call(X) is executed as if that term  appeared  textu-
  2690.  
  2691.      ally  in place of the call(X), except that any  cut (`!') occurring in
  2692.  
  2693.      X will remove only those choice points in X.  If  X  is  not   instan-
  2694.  
  2695.      tiated   as  described  above,  an  error  message is printed and call
  2696.  
  2697.      fails.
  2698.  
  2699.  
  2700.  
  2701.  
  2702.                                     39
  2703.  
  2704.  
  2705.  
  2706.  
  2707.  
  2708.  
  2709.  
  2710.  
  2711.  
  2712.  
  2713.  
  2714.  
  2715. X    (where X is a variable) Exactly the  same  as  call(X).   However,  we
  2716.  
  2717.      prefer  the explicit usage of call/1 as good programming practice, and
  2718.  
  2719.      the use of a top level variable subgoal elicits  a  warning  from  the
  2720.  
  2721.      compiler.
  2722.  
  2723.  
  2724. conlength(C,L)
  2725.  
  2726.  
  2727.      Succeeds if the length of the print name of the constant C (which  can
  2728.  
  2729.      be an atom, buffer or integer), in bytes, is L.  If C is a buffer (see
  2730.  
  2731.      Section 5.8), it is the length of the buffer; if C is an  integer,  it
  2732.  
  2733.      is the length of the decimal representation of that integer, i.e., the
  2734.  
  2735.      number of bytes that a $writename will use.
  2736.  
  2737.  
  2738. 5.6.  Sets
  2739.  
  2740.  
  2741.  
  2742.  
  2743.      When  there  are  many solutions to a  problem,  and  when  all  those
  2744.  
  2745. solutions  are  required   to   be   collected   together,   this   can  be
  2746.  
  2747. achieved  by  repeatedly backtracking  and gradually building up a list  of
  2748.  
  2749. the solutions.  The following evaluable predicates are provided to automate
  2750.  
  2751. this process.
  2752.  
  2753.  
  2754. setof(X,P,S)
  2755.  
  2756.      Read this as "S is the set of all instances of X   such   that  P   is
  2757.  
  2758.      provable''.   If  P  is  not  provable,  setof(X,P,S)  succeeds with S
  2759.  
  2760.      instantiated to the empty list [].  The term P  specifies  a  goal  or
  2761.  
  2762.      goals  as  in  call(P). S is a set of terms represented as  a list  of
  2763.  
  2764.      those terms, without duplicates, in the standard order for terms  (see
  2765.  
  2766.  
  2767.  
  2768.                                     40
  2769.  
  2770.  
  2771.  
  2772.  
  2773.  
  2774.  
  2775.  
  2776.  
  2777.  
  2778.  
  2779.  
  2780.  
  2781.      Section  5.7). If there are uninstantiated variables in P which do not
  2782.  
  2783.      also appear in X, then a  call  to  this   evaluable   predicate   may
  2784.  
  2785.      backtrack,  generating   alternative  values  for  S  corresponding to
  2786.  
  2787.      different instantiations of the free variables of P.  Variables occur-
  2788.  
  2789.      ring  in  P  will  not be treated as free if they are explicitly bound
  2790.  
  2791.      within P by an existential quantifier.  An existential  quantification
  2792.  
  2793.      is written:
  2794.  
  2795.          Y^Q
  2796.  
  2797.  
  2798.      meaning "there exists a Y such that Q is true", where  Y is some  Pro-
  2799.  
  2800.      log term (usually, a variable, or tuple or list of variables).
  2801.  
  2802.  
  2803. bagof(X,P,Bag)
  2804.  
  2805.      This is the same as setof except that the list (or alternative  lists)
  2806.  
  2807.      returned  will not be ordered,  and  may  contain duplicates.  If P is
  2808.  
  2809.      unsatisfiable, bagof succeeds binding Bag  to  the  empty  list.   The
  2810.  
  2811.      effect  of  this  relaxation is to save considerable time and space in
  2812.  
  2813.      execution.
  2814.  
  2815.  
  2816. findall(X,P,L)
  2817.  
  2818.      Similar to bagof/3, except that variables in P that do not occur in  X
  2819.  
  2820.      are  treated as local, and alternative lists are not returned for dif-
  2821.  
  2822.      ferent bindings of such variables.  The list L is, in  general,  unor-
  2823.  
  2824.      dered,  and  may  contain  duplicates.  If P is unsatisfiable, findall
  2825.  
  2826.      succeeds binding S to the empty list.
  2827.  
  2828.  
  2829. X^P
  2830.  
  2831.  
  2832.  
  2833.  
  2834.                                     41
  2835.  
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842.  
  2843.  
  2844.  
  2845.  
  2846.  
  2847.      The system recognises this as meaning "there exists an X  such that  P
  2848.  
  2849.      is  true",  and  treats  it as equivalent to call(P).  The use of this
  2850.  
  2851.      explicit existential quantifier outside the setof and bagof constructs
  2852.  
  2853.      is superfluous.
  2854.  
  2855.  
  2856. 5.7.  Comparison of Terms
  2857.  
  2858.  
  2859.  
  2860.  
  2861.      These  evaluable  predicates  are  meta-logical.    They  treat  unin-
  2862.  
  2863. stantiated  variables  as  objects  with  values  which  may  be  compared,
  2864.  
  2865. and  they  never instantiate those variables.  They should not be used when
  2866.  
  2867. what you really want is arithmetic comparison (Section 5.2) or unification.
  2868.  
  2869.  
  2870.      The  predicates  make reference to a standard total ordering of terms,
  2871.  
  2872. which is as follows:
  2873.  
  2874.  
  2875.      variables, in a standard order (roughly, oldest first - the order   is
  2876.  
  2877.      not related to the names of variables);
  2878.  
  2879.  
  2880.      numbers, from -"infinity" to +"infinity";
  2881.  
  2882.  
  2883.      atoms, in alphabetical (i.e. ASCII) order;
  2884.  
  2885.  
  2886.      complex  terms, ordered first by arity, then by the name of  principal
  2887.  
  2888.      functor, then by the arguments (in left-to-right order).
  2889.  
  2890.  
  2891.      For example, here is a list of terms in the standard order:
  2892.  
  2893.     [ X, -9, 1, fie, foe, fum, X = Y, fie(0,2), fie(1,1) ]
  2894.  
  2895.  
  2896. These are the basic predicates for comparison of arbitrary terms:
  2897.  
  2898.  
  2899.  
  2900.                                     42
  2901.  
  2902.  
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913. X == Y
  2914.  
  2915.  
  2916.      Tests if the terms currently instantiating X  and   Y  are   literally
  2917.  
  2918.      identical   (in  particular,  variables in equivalent positions in the
  2919.  
  2920.      two terms must be identical).  For example, the question
  2921.  
  2922.          | ?- X == Y.
  2923.  
  2924.  
  2925.      fails (answers "no") because X and  Y  are   distinct   uninstantiated
  2926.  
  2927.      variables.  However, the question
  2928.  
  2929.          | ?- X = Y, X == Y.
  2930.  
  2931.  
  2932.      succeeds because the first goal unifies the two  variables  (see  page
  2933.  
  2934.      42).
  2935.  
  2936.  
  2937. X \== Y
  2938.  
  2939.  
  2940.      Tests  if  the  terms  currently  instantiating  X  and   Y   are  not
  2941.  
  2942.      literally identical.
  2943.  
  2944.  
  2945. T1 @< T2
  2946.  
  2947.  
  2948.      Term T1 is before term T2 in the standard order.
  2949.  
  2950.  
  2951. T1 @> T2
  2952.  
  2953.  
  2954.      Term T1 is after term T2 in the standard order.
  2955.  
  2956.  
  2957. T1 @=< T2
  2958.  
  2959.  
  2960.      Term T1 is not after term T2 in the standard order.
  2961.  
  2962.  
  2963.  
  2964.  
  2965.  
  2966.                                     43
  2967.  
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974.  
  2975.  
  2976.  
  2977.  
  2978.  
  2979. T1 @>= T2
  2980.  
  2981.  
  2982.      Term T1 is not before term T2 in the standard order.
  2983.  
  2984.  
  2985.  
  2986.  
  2987. Some further predicates involving comparison of terms are:
  2988.  
  2989.  
  2990. compare(Op,T1,T2)
  2991.  
  2992.  
  2993.      The  result  of comparing terms T1 and T2 is Op,  where  the  possible
  2994.  
  2995.      values for Op are:
  2996.  
  2997.          `='   if T1 is identical to T2,
  2998.  
  2999.          `<'   if T1 is before T2 in the standard order,
  3000.  
  3001.          `>'   if T1 is after T2 in the standard order.
  3002.  
  3003.  
  3004.      Thus compare(=,T1,T2) is equivalent to T1 == T2.
  3005.  
  3006.  
  3007. sort(L1,L2)
  3008.  
  3009.  
  3010.      The elements of the list L1 are sorted into the  standard  order,  and
  3011.  
  3012.      any  identical  (i.e.  `==') elements are merged,  yielding  the  list
  3013.  
  3014.      L2.
  3015.  
  3016.  
  3017. 5.8.  Buffers
  3018.  
  3019.  
  3020.  
  3021.  
  3022.      SB-Prolog supports the concept of buffers.  A  buffer  is  actually  a
  3023.  
  3024. constant and the characters that make up the buffer is the name of the con-
  3025.  
  3026. stant.  However, the symbol table entry for a buffer is not hashed and thus
  3027.  
  3028. is  not  added  to the obj-list, so two different buffers will never unify.
  3029.  
  3030.  
  3031.  
  3032.                                     44
  3033.  
  3034.  
  3035.  
  3036.  
  3037.  
  3038.  
  3039.  
  3040.  
  3041.  
  3042.  
  3043.  
  3044.  
  3045. Buffers can be allocated either in permanent space or on the heap.  Buffers
  3046.  
  3047. in  permanent space stay there forever; buffers on the heap are deallocated
  3048.  
  3049. when the ``allocate buffer'' goal is backtracked over.
  3050.  
  3051.  
  3052.      A buffer allocated on the heap can either be a simple  buffer,  or  it
  3053.  
  3054. can  be  allocated as a subbuffer of another buffer already on the heap.  A
  3055.  
  3056. subbuffer will be deallocated when its superbuffer is deallocated.
  3057.  
  3058.  
  3059.      There are occasions when it is not known, in advance, exactly how much
  3060.  
  3061. space  will be required and so how big a buffer should be allocated.  Some-
  3062.  
  3063. times this problem can be overcome by allocating a large buffer  and  then,
  3064.  
  3065. after  using  as much as is needed, returning the rest of the buffer to the
  3066.  
  3067. system. This can be done, but only  under  very  limited  circumstances:  a
  3068.  
  3069. buffer  is  allocated  from  the end of the permanent space, the top of the
  3070.  
  3071. heap, or from the next available space in the superbuffer; if no more space
  3072.  
  3073. has  been  used  beyond the end of the buffer, a tail portion of the buffer
  3074.  
  3075. can be returned to the system. This operation is  called  ``trimming''  the
  3076.  
  3077. buffer.
  3078.  
  3079.  
  3080.      The following is a list of library predicates  for  buffer  management
  3081.  
  3082. (predicates whose names begin with `$' are low-level routines intended only
  3083.  
  3084. for serious hackers willing to put up with little or no protection):
  3085.  
  3086.  
  3087.  
  3088.  
  3089. alloc_perm(SizeBuff)
  3090.  
  3091.      Allocates a buffer with a length Size in the permanent (i.e.  program)
  3092.  
  3093.      area.  Size must be bound to a number. On successful return, Buff will
  3094.  
  3095.      be bound to the allocated buffer.  The buffer, being in the  permanent
  3096.  
  3097.  
  3098.                                     45
  3099.  
  3100.  
  3101.  
  3102.  
  3103.  
  3104.  
  3105.  
  3106.  
  3107.  
  3108.  
  3109.  
  3110.  
  3111.      area, is never de-allocated.
  3112.  
  3113.  
  3114. alloc_heap(SizeBuff)
  3115.  
  3116.      Allocates a buffer of size Size on the heap  and  binds  Buff  to  it.
  3117.  
  3118.      Since it is on the heap, it will be deallocated on backtracking.
  3119.  
  3120.  
  3121. $alloc_buff(Size,Buff,Type,Supbuff,Retcode)
  3122.  
  3123.  
  3124.      Allocates a buffer.  Size is the length (in bytes) of  the  buffer  to
  3125.  
  3126.      allocate;  Buff  is the buffer allocated, and should be unbound at the
  3127.  
  3128.      time of the call; Type indicates where to allocate the buffer: a value
  3129.  
  3130.      of  0 indicates that the buffer is to be allocated in permanent space,
  3131.  
  3132.      1 that it should be on the heap, and 2 indicates  that  it  should  be
  3133.  
  3134.      allocated  from a larger heap buffer; Supbuff is the larger  buffer to
  3135.  
  3136.      allocate a subbuffer out of, and is only looked at  if  the  value  of
  3137.  
  3138.      Type is 2; Retcode is the return code: a value of 0 indicates that the
  3139.  
  3140.      buffer has been allocated, while a  value  of  1  indicates  that  the
  3141.  
  3142.      buffer  could  not  be  allocated due to lack of space.  The arguments
  3143.  
  3144.      SizeType, and Supbuff (if Type = 2) are input arguments, and  should
  3145.  
  3146.      be  bound  at  the time of the call; Buff and Retcode are output argu-
  3147.  
  3148.      ments, and should be unbound at the time of the call.
  3149.  
  3150.  
  3151. trimbuff(TypeBuffNewlen)
  3152.  
  3153.      This allows (in some very restricted circumstances)  the  changing  of
  3154.  
  3155.      the  size  of a buffer. Type is 0 if the buffer is permanent, 1 if the
  3156.  
  3157.      buffer is on the heap. Buff is the buffer.  Newlen is an integer:  the
  3158.  
  3159.      size  (which should be smaller than the original length of the buffer)
  3160.  
  3161.      to make the buffer. If the buffer is at the top of the heap  (if  heap
  3162.  
  3163.  
  3164.                                     46
  3165.  
  3166.  
  3167.  
  3168.  
  3169.  
  3170.  
  3171.  
  3172.  
  3173.  
  3174.  
  3175.  
  3176.  
  3177.      buffer)  or  the end of the program area (if permanent) then the heap-
  3178.  
  3179.      top (or program area top) will be readjusted down.  The length of  the
  3180.  
  3181.      buffer  will  be  modified to Newlen.  This is (obviously) a very low-
  3182.  
  3183.      level primitive and is for hackers only to implement grungy stuff.
  3184.  
  3185.  
  3186. $trim_buff(Size,Buff,Type,Supbuff)
  3187.  
  3188.  
  3189.      Trims the buffer Buff (possibly contained in superbuffer  Supbuff)  of
  3190.  
  3191.      type  Type to Size bytes. The value of Type indicates where the buffer
  3192.  
  3193.      is located: a value of 0 denotes a buffer in permanent space, a  value
  3194.  
  3195.      of  1  a  buffer on the heap, and a value of 2 a buffer within another
  3196.  
  3197.      buffer (the superbuffer).  All the arguments are input arguments,  and
  3198.  
  3199.      should  be  instantiated at the time of call (with the possible excep-
  3200.  
  3201.      tion of Supbuff, which is looked at only if Type is 2).  The  internal
  3202.  
  3203.      length of the buffer Buff is changed to Size.
  3204.  
  3205.  
  3206. conlength(Constant,Length)
  3207.  
  3208.  
  3209.      Succeeds if the length of the print  name  of  the  constant  Constant
  3210.  
  3211.      (which  can  be  an atom, buffer or integer), in bytes, is Length.  If
  3212.  
  3213.      Constant is a buffer, it is the length of the buffer; if  Constant  is
  3214.  
  3215.      an  integer,  it  is  the length of the decimal representation of that
  3216.  
  3217.      integer, i.e., the number of bytes that a $writename will use.
  3218.  
  3219.  
  3220. 5.9.  Modification of the Program
  3221.  
  3222.  
  3223.  
  3224.  
  3225.      The predicates defined in this section allow modification of the  pro-
  3226.  
  3227. gram  as  it  is  actually  running.   Clauses  can be added to the program
  3228.  
  3229.  
  3230.                                     47
  3231.  
  3232.  
  3233.  
  3234.  
  3235.  
  3236.  
  3237.  
  3238.  
  3239.  
  3240.  
  3241.  
  3242.  
  3243. (asserted) or removed from the program (retracted).  At the  lowest  level,
  3244.  
  3245. the  system  supports the asserting of clauses with upto one literal in the
  3246.  
  3247. body.  It does this by allocating a  buffer  and  compiling  code  for  the
  3248.  
  3249. clause  into  that  buffer.  Such a buffer is called a ``clause reference''
  3250.  
  3251. (clref).  The clref is then added to a  chain  of  clrefs.   The  chain  of
  3252.  
  3253. clrefs  has  a  header, which is a small buffer called a ``predicate refer-
  3254.  
  3255. ence'' (prref), which contains pointers to the beginning  and  end  of  its
  3256.  
  3257. (prref),  which  contains pointers to the beginning and end of its chain of
  3258.  
  3259. clrefs.  Clause references are quite similar to ``database references''  of
  3260.  
  3261. C-Prolog, and can be called.
  3262.  
  3263.  
  3264.      A prref is normally associated with a predicate  symbol  and  contains
  3265.  
  3266. the  clauses  that have been asserted for that symbol.  However, prrefs can
  3267.  
  3268. be constructed and clrefs added to them, and such clauses can  be  can   be
  3269.  
  3270. constructed  and  clrefs added to them, and such clauses can be called, all
  3271.  
  3272. without associating that prref with any  particular  predicate  symbol.   A
  3273.  
  3274. predicate  symbol  that   has  an  associated prref is called a ``dynamic''
  3275.  
  3276. predicate (other  predicate types are ``compiled'' and ``undefined'').
  3277.  
  3278.  
  3279.      There are options as to where clrefs are  allocated and  how they  are
  3280.  
  3281. linked  to  a  chain  of   clrefs  associated  with a  prref.   A clref can
  3282.  
  3283. either be allocated in permanent space (the normal  arrangement)  or  as  a
  3284.  
  3285. sub-buffer  of  a superbuffer on the heap.  When adding a  clref to a prref
  3286.  
  3287. chain, it can be be added at the beginning  of the  chain or at the end  of
  3288.  
  3289. the  chain.   In  addition,  one  of  the argument positions can be used to
  3290.  
  3291. index, so that subsequent retrievals will try  to index on that position.
  3292.  
  3293.  
  3294.  
  3295.  
  3296.                                     48
  3297.  
  3298.  
  3299.  
  3300.  
  3301.  
  3302.  
  3303.  
  3304.  
  3305.  
  3306.  
  3307.  
  3308.  
  3309. assert(C)
  3310.  
  3311.  
  3312.      The current instance of C is interpreted as a clause and is  added  to
  3313.  
  3314.      the  program  (with new private variables replacing any uninstantiated
  3315.  
  3316.      variables), at the end of the list of clauses for that  predicate.   C
  3317.  
  3318.      must be instantiated to a non-variable.
  3319.  
  3320.  
  3321. assert(COpts)
  3322.  
  3323.  
  3324.      As for assert/1, with Opts a list of options.  The  options  currently
  3325.  
  3326.      recognized are:
  3327.  
  3328.  
  3329.    first
  3330.  
  3331.  
  3332.      If on, causes the asserted clause to be added as the first  clause  of
  3333.  
  3334.      the database.  Default: off.
  3335.  
  3336.  
  3337.    index
  3338.  
  3339.  
  3340.      If on, causes an index to be generated on the first  argument  of  the
  3341.  
  3342.      clause.  Default: off.
  3343.  
  3344.  
  3345.    index(N)
  3346.  
  3347.  
  3348.      If on, causes an index to be generated on the N[th]  argument  of  the
  3349.  
  3350.      clause.  Default: off.
  3351.  
  3352.  
  3353.    q
  3354.  
  3355.  
  3356.      ``quick''.  If on, invokes an assertion algorithm that is simpler  and
  3357.  
  3358.      more efficient than the general one.  However, the code generated with
  3359.  
  3360.  
  3361.  
  3362.                                     49
  3363.  
  3364.  
  3365.  
  3366.  
  3367.  
  3368.  
  3369.  
  3370.  
  3371.  
  3372.  
  3373.  
  3374.  
  3375.      the simpler algorithm may not be correct if there are  repeated  vari-
  3376.  
  3377.      ables within compound terms in the body of the clause, e.g. in
  3378.  
  3379.                  p(X) :- q([X, X]).
  3380.  
  3381.  
  3382.      Default: off.
  3383.  
  3384.  
  3385. asserti(C,N)
  3386.  
  3387.  
  3388.      The current instance of C, interpreted as a clause, is asserted to the
  3389.  
  3390.      program  with  an index on its N[th] argument.  If N is zero, no index
  3391.  
  3392.      is created.
  3393.  
  3394.  
  3395. asserta(C)
  3396.  
  3397.  
  3398.      Similar to assert(C), except that the new  clause  becomes  the  first
  3399.  
  3400.      clause of the procedure concerned.
  3401.  
  3402.  
  3403. asserta(CRef)
  3404.  
  3405.  
  3406.      Similar to asserta(C), but also unifies Ref with the clause  reference
  3407.  
  3408.      of the clause asserted.
  3409.  
  3410.  
  3411. assertz(C)
  3412.  
  3413.  
  3414.      Similar to assert(C), except that the  new  clause  becomes  the  last
  3415.  
  3416.      clause of the procedure concerned.
  3417.  
  3418.  
  3419. assertz(CRef)
  3420.  
  3421.  
  3422.      Similar to assertz(C), but also unifies Ref with the clause  reference
  3423.  
  3424.      of the clause asserted.
  3425.  
  3426.  
  3427.  
  3428.                                     50
  3429.  
  3430.  
  3431.  
  3432.  
  3433.  
  3434.  
  3435.  
  3436.  
  3437.  
  3438.  
  3439.  
  3440.  
  3441. assert_union(PQ)
  3442.  
  3443.  
  3444.      The clauses for Q are added to the clauses for P.   For  example,  the
  3445.  
  3446.      call
  3447.  
  3448.                  | ?- assert_union(p(X,Y),q(X,Y)).
  3449.  
  3450.  
  3451.      has the effect of adding the rule
  3452.  
  3453.          p(X,Y) :- q(X,Y).
  3454.  
  3455.  
  3456.      as the last rule defining p/2.  If P is not defined, it results in the
  3457.  
  3458.      call to Q being the only clause for P.
  3459.  
  3460.  
  3461.  
  3462.      The variables in the arguments to $assert_union/2 are not significant,
  3463.  
  3464.      e.g. the above would have been equivalent to
  3465.  
  3466.                  | ?- assert_union(p(Y,X),q(X,Y)).
  3467.          or
  3468.                  | ?- assert_union(p(_,_),q(_,_)).
  3469.  
  3470.  
  3471.      However, the arities of the two predicates involved must  match,  e.g.
  3472.  
  3473.      even though the goal
  3474.  
  3475.                  | ?- assert_union(p(X,Y), r(X,Y,Z)).
  3476.  
  3477.  
  3478.      will succeed, the predicate p/2 will not in  any  way  depend  on  the
  3479.  
  3480.      clauses for r/3.
  3481.  
  3482.  
  3483. $assert(Clause,AZ,Index,Clref)
  3484.  
  3485.  
  3486.      Asserts a clause to a predicate.  Clause is the clause to assert.   AZ
  3487.  
  3488.      is  0  for  insertion as the first clause, 1 for insertion as the last
  3489.  
  3490.      clause.  Index is the number of the argument on which to  index (0 for
  3491.  
  3492.  
  3493.  
  3494.                                     51
  3495.  
  3496.  
  3497.  
  3498.  
  3499.  
  3500.  
  3501.  
  3502.  
  3503.  
  3504.  
  3505.  
  3506.  
  3507.      no indexing).  Clref is returned  as the  clause reference of the fact
  3508.  
  3509.      newly asserted.  If  the  main  functor  symbol  of  Clause  has  been
  3510.  
  3511.      declared (by $assertf_alloc_t/2, see below) to have its clauses on the
  3512.  
  3513.      heap, the clref will be allocated there. If the  predicate  symbol  of
  3514.  
  3515.      Clause  is  undefined, it will be initialized and Clause added. If the
  3516.  
  3517.      predicate symbol has compiled clauses, it is  first  converted  to  be
  3518.  
  3519.      dynamic  (see  symtype/2, Section 5.10) by adding a special clref that
  3520.  
  3521.      calls the compiled clauses.  FactAZ and Index are  input  arguments,
  3522.  
  3523.      and  should  be  instantiated  at the time of call; Clref is an output
  3524.  
  3525.      argument, and should be uninstantiated at the time of call.
  3526.  
  3527.  
  3528. $assertf_alloc_t(Palist,Size)
  3529.  
  3530.  
  3531.      Declares that each predicate in the  list  Palist  of  predicate/arity
  3532.  
  3533.      pairs  (terms of the form `/'(P,N) where P is a predicate symbol and N
  3534.  
  3535.      the arity of P) is to have any facts asserted  to  them  stored  in  a
  3536.  
  3537.      buffer  on  the  heap,  to be allocated here.  This allocates a super-
  3538.  
  3539.      buffer of size Size on the heap.  Future assertions  to  these  predi-
  3540.  
  3541.      cates  will  have their clauses put in this buffer.  When this call is
  3542.  
  3543.      backtracked over, any clauses asserted to these predicates are deallo-
  3544.  
  3545.      cated, and a subsequent call to any of those predicates will cause the
  3546.  
  3547.      simulator to report an error and fail.  Both Palist and Size are input
  3548.  
  3549.      arguments, and should be instantiated at the time of call.
  3550.  
  3551.  
  3552. retract(Head)
  3553.  
  3554.  
  3555.      The first clause in the program whose head  matches  Head  is  erased.
  3556.  
  3557.      This  predicate  may  be  used in a non-deterministic fashion, i.e. it
  3558.  
  3559.  
  3560.                                     52
  3561.  
  3562.  
  3563.  
  3564.  
  3565.  
  3566.  
  3567.  
  3568.  
  3569.  
  3570.  
  3571.  
  3572.  
  3573.      will successively backtrack to retract clauses whose heads match Head.
  3574.  
  3575.      Head  must be initially instantiated to a non-variable. In the current
  3576.  
  3577.      implementation, retract  works  only  for  asserted  (e.g.  consulted)
  3578.  
  3579.      clauses.
  3580.  
  3581.  
  3582. abolish(P)
  3583.  
  3584.  
  3585.      Completely remove all clauses for the procedure  with  head  P  (which
  3586.  
  3587.      should be a term). For example, the goal
  3588.  
  3589.          | ?- abolish( p(_, _, _) ).
  3590.  
  3591.  
  3592.      removes all clauses for the predicate p/3.
  3593.  
  3594.  
  3595. abolish(PN)
  3596.  
  3597.  
  3598.      Completely remove all clauses for the predicate P (which should be  an
  3599.  
  3600.      atom) with arity N (which should be an integer).
  3601.  
  3602.  
  3603. call_ref(CallRef)
  3604.  
  3605.  
  3606.      Calls the predicate whose database reference (prref) is Ref, using the
  3607.  
  3608.      literal Call as the call.  This is similar to call_ref(CallRef, 0).
  3609.  
  3610.  
  3611. call_ref(CallRefTr)
  3612.  
  3613.  
  3614.      Calls the predicate whose database reference (prref) is Ref, using the
  3615.  
  3616.      literal  Call  as the call.  Tr must be either 0 or 1: if Tr is 0 then
  3617.  
  3618.      the call Call is made assuming  the  ``trust''  optimization  will  be
  3619.  
  3620.      made;  if Tr is 1 then the ``trust'' optimization is not used, so that
  3621.  
  3622.      any new fact added before final failure will be seen by Call.   (Also,
  3623.  
  3624.  
  3625.  
  3626.                                     53
  3627.  
  3628.  
  3629.  
  3630.  
  3631.  
  3632.  
  3633.  
  3634.  
  3635.  
  3636.  
  3637.  
  3638.  
  3639.      this currently does not take advantage of any indexing that might have
  3640.  
  3641.      been constructed.  CallRef and  Tr  are  all  input  arguments,  and
  3642.  
  3643.      should be instantiated at the time of call.
  3644.  
  3645.  
  3646.  
  3647.  
  3648. The basic library predicates that support the manipulation  of  prrefs  and
  3649.  
  3650. clrefs are as follows:
  3651.  
  3652.  
  3653. $db_new_prref(Prref,Where,Supbuff)
  3654.  
  3655.  
  3656.      Creates an empty Prref, i.e. one with no facts in it.  If  called,  it
  3657.  
  3658.      will  simply  fail.   Where  indicates where the prref should be allo-
  3659.  
  3660.      cated:  a value of 0 indicates the permanent area, while a value of  2
  3661.  
  3662.      indicates  that  it is to be allocated as a subbuffer.  Supbuff is the
  3663.  
  3664.      superbuffer from which to allocate Prref if Where is 2.  Where  should
  3665.  
  3666.      be  instantiated  at the time of call, while Prref should be uninstan-
  3667.  
  3668.      tiated; in addition, if Where is 2, Supbuff should be instantiated  at
  3669.  
  3670.      the time of call.
  3671.  
  3672.  
  3673. $db_assert_fact(Fact,Prref,AZ,Index,Clref,Where,Supbuff)
  3674.  
  3675.  
  3676.      Fact is a fact to be asserted; Prref is a predicate reference to which
  3677.  
  3678.      to  add  the asserted fact; AZ is either 0, indicating the fact should
  3679.  
  3680.      be inserted as the first clause in Prref, or 1, indicating  it  should
  3681.  
  3682.      be  inserted  as the last; Index is 0 if no index is to be built, or n
  3683.  
  3684.      if an index on the n[th] argument of the fact is to be used.  (Assert-
  3685.  
  3686.      ing at the beginning of the chain with indexing is not yet supported.)
  3687.  
  3688.      Where indicates where the clref is to  be  allocated:  a  value  of  0
  3689.  
  3690.  
  3691.  
  3692.                                     54
  3693.  
  3694.  
  3695.  
  3696.  
  3697.  
  3698.  
  3699.  
  3700.  
  3701.  
  3702.  
  3703.  
  3704.  
  3705.      indicates  that it should be in the permanent area, while a value of 2
  3706.  
  3707.      indicates that it should be  allocated  as  a  subbuffer  of  Supbuff.
  3708.  
  3709.      Clref  is returned and it  is  the  clause reference  of the  asserted
  3710.  
  3711.      fact.   FactPrrefAZIndex and  Where  are  input  arguments,  and
  3712.  
  3713.      should  be  instantiated at the time of call; in addition, if Where is
  3714.  
  3715.      2, then Supbuff should also be instantiated.  Clref is an output argu-
  3716.  
  3717.      ment, and should be uninstantiated at the time of call.
  3718.  
  3719.  
  3720. $db_add_clref(Fact,Prref,AZ,Index,Clref,Where,Supbuff)
  3721.  
  3722.  
  3723.      Adds the clref Clref to the prref PrrefFact is  the  fact  that  has
  3724.  
  3725.      been  compiled  into  Clref (used only to get the arity and for index-
  3726.  
  3727.      ing).  The other parameters are as for $db_assert_fact/7.
  3728.  
  3729.  
  3730. $db_call_prref(Call,Prref)
  3731.  
  3732.  
  3733.      Calls the prref Prref using the literal Call as the call.  The call is
  3734.  
  3735.      done  by  simply  branching  to  the first clause.  New facts added to
  3736.  
  3737.      Prref after the last fact has been retrieved by Call, but before  Call
  3738.  
  3739.      is  failed  through,  will not be used.  Both Call and Prref are input
  3740.  
  3741.      arguments, and should be instantiated at the time of call.
  3742.  
  3743.  
  3744. $db_call_prref_s(Call,Prref)
  3745.  
  3746.  
  3747.      This also  calls the prref Prref using Call as the call.  The  differ-
  3748.  
  3749.      ence  from  $db_call_prref  is  that  this  does not use the ``trust''
  3750.  
  3751.      optimization, so that any new fact added before final failure will  be
  3752.  
  3753.      seen  by  Call.   (Also, this currently does not take advantage of any
  3754.  
  3755.      indexing that might have been constructed, while $db_call_prref does.)
  3756.  
  3757.  
  3758.                                     55
  3759.  
  3760.  
  3761.  
  3762.  
  3763.  
  3764.  
  3765.  
  3766.  
  3767.  
  3768.  
  3769.  
  3770.  
  3771.      Both Call and Prref are input arguments, and should be instantiated at
  3772.  
  3773.      the time of call.
  3774.  
  3775.  
  3776. $db_get_clauses(Prref,Clref,Dir)
  3777.  
  3778.  
  3779.      This returns, nondeterministically, all the  clause  references  Clref
  3780.  
  3781.      for  clauses  asserted  to  prref  Prref.  If Dir is 0, then the first
  3782.  
  3783.      clref on the list is returned first;  if  Dir  is  1,  then  they  are
  3784.  
  3785.      returned  in  reverse  order.   Prref and Dir are input arguments, and
  3786.  
  3787.      should be instantiated at the time of call; Clref is an  output  argu-
  3788.  
  3789.      ment, and should be uninstantiated at the time of call.
  3790.  
  3791.  
  3792. $db_kill_clause(Clref)
  3793.  
  3794.  
  3795.      This predicate retracts the fact referenced by clref Clref.   It  does
  3796.  
  3797.      this  by  simply  making  the  first  instruction of the clause a fail
  3798.  
  3799.      instruction.  This means that this clause will  fail  whenever  it  is
  3800.  
  3801.      called  in  the future, no matter how it is accessed.  Clref should be
  3802.  
  3803.      instantiated at the time of call.
  3804.  
  3805.  
  3806. 5.10.  Environmental
  3807.  
  3808.  
  3809.  
  3810.  
  3811. op(prioritytypename)
  3812.  
  3813.  
  3814.      Treat name as an operator of the stated type and priority (see Section
  3815.  
  3816.      3.2).   name  may  also  be  a  list  of names, in which all are to be
  3817.  
  3818.      treated as operators of the stated type and priority.
  3819.  
  3820.  
  3821.  
  3822.  
  3823.  
  3824.                                     56
  3825.  
  3826.  
  3827.  
  3828.  
  3829.  
  3830.  
  3831.  
  3832.  
  3833.  
  3834.  
  3835.  
  3836.  
  3837. break
  3838.  
  3839.  
  3840.      Causes the current execution to be suspended  at  the  next  procedure
  3841.  
  3842.      call.   Then  the  message  ``[ Break (level 1) ]'' is displayed.  The
  3843.  
  3844.      interpreter is then ready to accept input as though it was at the  top
  3845.  
  3846.      level  (except  that  at break level n > 0, the prompt is ``n: ?- '').
  3847.  
  3848.      If another call of break is encountered, it moves up to level  2,  and
  3849.  
  3850.      so  on.   To  close  the  break  and  resume  the  execution which was
  3851.  
  3852.      suspended, type the END-OF-INPUT character.  Execution will be resumed
  3853.  
  3854.      at the procedure call where it had been suspended.  Alternatively, the
  3855.  
  3856.      suspended execution can be aborted by calling the evaluable  predicate
  3857.  
  3858.      abort, which causes a return to the top level.
  3859.  
  3860.  
  3861. abort
  3862.  
  3863.  
  3864.      Aborts the current execution, taking you back to top level.
  3865.  
  3866.  
  3867. save(F)
  3868.  
  3869.  
  3870.      The system saves the current state of the system into file F.
  3871.  
  3872.  
  3873. restore(F)
  3874.  
  3875.  
  3876.      The system restores the saved state in file F to be the current state.
  3877.  
  3878.      One  restriction  imposed by the current system is that various system
  3879.  
  3880.      parameters (e.g. stack sizes, permanent space, heap  space,  etc.)  of
  3881.  
  3882.      the saved state have to be the same as that of the current invocation.
  3883.  
  3884.      Thus, it is not possible to save a  state  from  an  invocation  where
  3885.  
  3886.      50000  words  of  permanent space had been allocated, and then restore
  3887.  
  3888.  
  3889.  
  3890.                                     57
  3891.  
  3892.  
  3893.  
  3894.  
  3895.  
  3896.  
  3897.  
  3898.  
  3899.  
  3900.  
  3901.  
  3902.  
  3903.      the same state in an invocation with 100000 words of permanent space.
  3904.  
  3905.  
  3906. cputime(X)
  3907.  
  3908.  
  3909.      Unifies X with the time elapsed, in milliseconds, since the system was
  3910.  
  3911.      started up.
  3912.  
  3913.  
  3914. $getenv(Var,Val)
  3915.  
  3916.  
  3917.      Val is unified with the value of the Unix  environment  variable  Var.
  3918.  
  3919.      Fails is Var is undefined.
  3920.  
  3921.  
  3922. statistics
  3923.  
  3924.  
  3925.      Prints out the current allocations and amounts of space used for  each
  3926.  
  3927.      of  the four main areas: the permanent area, the local stack, the glo-
  3928.  
  3929.      bal stack and the trail stack.  Does not work well unless the  simula-
  3930.  
  3931.      tor has been called with the -s option (see Section 7.2).
  3932.  
  3933.  
  3934. nodynload(PN)
  3935.  
  3936.  
  3937.      Flags the predicate P with arity N as one that should not be attempted
  3938.  
  3939.      to  be  dynamically  loaded  if  it  is  undefined.  If a predicate so
  3940.  
  3941.      flagged is undefined when a call to it is encountered, the call  fails
  3942.  
  3943.      quietly without trying to invoke the dynamic loader or giving an error
  3944.  
  3945.      message.  P and N should be instantiated to an atom  and  an  integer,
  3946.  
  3947.      respectively, at the time of call to nodynload/2.
  3948.  
  3949.  
  3950. symtype(T,N)
  3951.  
  3952.  
  3953.  
  3954.  
  3955.  
  3956.                                     58
  3957.  
  3958.  
  3959.  
  3960.  
  3961.  
  3962.  
  3963.  
  3964.  
  3965.  
  3966.  
  3967.  
  3968.  
  3969.      Unifies N with the ``internal type'' of the principal functor  of  the
  3970.  
  3971.      term  T,  which  must  be  instantiated at the time of the call.  N is
  3972.  
  3973.      bound to 0 if T does not have an entry point defined (i.e.  cannot  be
  3974.  
  3975.      executed); to 1 if the principal functor of T is ``dynamic'', i.e. has
  3976.  
  3977.      asserted code; to 2 if the principal  functor  for  T  is  a  compiled
  3978.  
  3979.      predicate;  and  3  if  T denotes a buffer.  Thus, for example, if the
  3980.  
  3981.      predicate p/2 is a compiled predicate which has been loaded  into  the
  3982.  
  3983.      system, the goal
  3984.  
  3985.                  | ?- symtype(p(_,_), X).
  3986.  
  3987.  
  3988.      will succeed binding X to 2; on the other hand, the goal
  3989.  
  3990.                  | ?- assert(q(a,b,c)), symtype(q(_,_,_), X).
  3991.  
  3992.  
  3993.      will succeed binding X to 1.
  3994.  
  3995.  
  3996. system(Call)
  3997.  
  3998.  
  3999.      Calls the operating system with the atom Call as argument.  For  exam-
  4000.  
  4001.      ple, the call
  4002.  
  4003.          | ?- system('ls').
  4004.  
  4005.  
  4006.      will produce a directory listing.  Since system/1 is executed by fork-
  4007.  
  4008.      ing off a shell process, it cannot be used, for example, to change the
  4009.  
  4010.      working directory of the simulator.
  4011.  
  4012.  
  4013. syscall(N,Args,Res)
  4014.  
  4015.  
  4016.      Executes the Unix system  call  number  N  with  arguments  Args,  and
  4017.  
  4018.      returns  the result in ResN is an integer, and Args a Prolog list of
  4019.  
  4020.  
  4021.  
  4022.                                     59
  4023.  
  4024.  
  4025.  
  4026.  
  4027.  
  4028.  
  4029.  
  4030.  
  4031.  
  4032.  
  4033.  
  4034.  
  4035.      the arguments to the system call.  For example, to execute the  system
  4036.  
  4037.      call  ``creat(File,Mode)'',  knowing  that  the syscall number for the
  4038.  
  4039.      Unix command creat(2) is 8, we execute the goal
  4040.  
  4041.          | ?- syscall(8, [FileMode], Des).
  4042.  
  4043.  
  4044.      where Des is the file  descriptor  returned  by  creat.   The  syscall
  4045.  
  4046.      numbers for some Unix system calls are given in Table 2.
  4047.  
  4048.  
  4049. 5.11.  Global Values
  4050.  
  4051.  
  4052.  
  4053.  
  4054.      SB-Prolog has some primitives that permit the programmer to manipulate
  4055.  
  4056. global  values.   These  are  provided primarily as an efficiency hack, and
  4057.  
  4058. needless to say, should be used with a great deal of care.
  4059.  
  4060.  
  4061. _________________________________________________________________________ 
  4062.     exit                      1     |       fork                     2   |
  4063.     read                      3     |       write                    4   |
  4064.     open                      5     |       close                    6   |
  4065.     creat                     8     |       link                     9   |
  4066.     unlink                   10     |       chdir                   12   |
  4067.     chmod                    15     |       lseek                   19   |
  4068.     access                   33     |       kill                    37   |
  4069.     wait                     84     |       socket                  97   |
  4070.     connect                  98     |       accept                  99   |
  4071.     send                    101     |       recv                   102   |
  4072.     bind                    104     |       setsockopt             105   |
  4073.     listen                  106     |       recvmsg                113   |
  4074.     sendmsg                 114     |       getsockopt             118   |
  4075.     recvfrom                125     |       sendto                 133   |
  4076.     socketpair              135     |       mkdir                  136   |
  4077.     rmdir                   137     |       getsockname            150   |
  4078. ____________________________________|____________________________________|
  4079.  
  4080.  
  4081.            Table 2: Some Syscall Numbers for Unix Systems Calls
  4082.  
  4083.  
  4084.  
  4085.  
  4086.  
  4087.  
  4088.                                     60
  4089.  
  4090.  
  4091.  
  4092.  
  4093.  
  4094.  
  4095.  
  4096.  
  4097.  
  4098.  
  4099.  
  4100.  
  4101. globalset(Term)
  4102.  
  4103.  
  4104.      Allows the user to save a global value.  Term must be bound to a  com-
  4105.  
  4106.      pound  term, say p(V). V must be a number or a constant or a variable.
  4107.  
  4108.      If V is a number or a constant, the effect of globalset(p(V))  can  be
  4109.  
  4110.      described as:
  4111.  
  4112.          retract(p(_)), assert(p(V)).
  4113.  
  4114.  
  4115.      I.e., p is a predicate that when called will, from now on (until  some
  4116.  
  4117.      other  change  by globalset/1), deterministically return V.  If V is a
  4118.  
  4119.      variable, the effect is to make V a global  variable  whose  value  is
  4120.  
  4121.      accessible  by  calling  p. For example, executing ``globalset(p(X))''
  4122.  
  4123.      makes X a global variable. X can be set by unification with some other
  4124.  
  4125.      term.  On backtracking, X will be restored to its earlier value.
  4126.  
  4127.  
  4128. gennum(Newnum)
  4129.  
  4130.  
  4131.      gennum/1 sets its argument to a new integer every time it is invoked.
  4132.  
  4133.  
  4134. gensym(C,Newsym)
  4135.  
  4136.  
  4137.      gensym/2 sets its second argument to an atom whose  name  is  made  by
  4138.  
  4139.      concatenating  the  name  of  the atom C to the current gennum number.
  4140.  
  4141.      This new constant is bound to Newsym.  For  example,  if  the  current
  4142.  
  4143.      gennum number is 37, then the call
  4144.  
  4145.          | ?- gensym(aaa,X)
  4146.  
  4147.  
  4148.      will succeed binding X to the atom `aaa37'.
  4149.  
  4150.  
  4151.  
  4152.  
  4153.  
  4154.                                     61
  4155.  
  4156.  
  4157.  
  4158.  
  4159.  
  4160.  
  4161.  
  4162.  
  4163.  
  4164.  
  4165.  
  4166.  
  4167. 6.  Debugging
  4168.  
  4169.  
  4170.  
  4171.  
  4172. 6.1.  High Level Tracing
  4173.  
  4174.  
  4175.  
  4176.  
  4177.      The preferred method of tracing execution  is  through  the  predicate
  4178.  
  4179. trace/1.   This predicate takes as argument a term P/N, where P is a predi-
  4180.  
  4181. cate name and N its arity, and sets a ``trace point'' on the  corresponding
  4182.  
  4183. predicate; it can also be given a list of such terms, in which case a trace
  4184.  
  4185. point is set on each member of the list.  For example, executing
  4186.  
  4187.             | ?- trace(pred1/2), trace([pred2/3, pred3/2]).
  4188.  
  4189.  
  4190. sets trace points on predicates pred1/2, pred2/3 and pred3/2.   Only  those
  4191.  
  4192. predicates are traced that have trace points set on them.
  4193.  
  4194.  
  4195.      If all the predicates in a file are to be traced, it is  usually  con-
  4196.  
  4197. venient to use the PredList parameter of compile/4 or consult/3, e.g.:
  4198.  
  4199.             | ?- compile(foo, 'foo.out', [t,v], Preds), load('foo.out'), trace(Preds).
  4200.     or
  4201.             | ?- consult(foo, [v], Preds), trace(Preds).
  4202.  
  4203.  
  4204. Notice that in the first case, the t  compiler  option  (see  Section  8.3)
  4205.  
  4206. should  be  specified  in order to turn off certain assembler optimizations
  4207.  
  4208. and facilitate tracing.  In  the  second  case,  the  same  effect  may  be
  4209.  
  4210. achieved by specifying the t option to consult.
  4211.  
  4212.  
  4213.      The trace points set on predicates may be overwritten by loading  byte
  4214.  
  4215. code  files  via load/1, and in this case it may be necessary to explicitly
  4216.  
  4217. set trace points again on the loaded predicates.  This does not happen with
  4218.  
  4219.  
  4220.                                     62
  4221.  
  4222.  
  4223.  
  4224.  
  4225.  
  4226.  
  4227.  
  4228.  
  4229.  
  4230.  
  4231.  
  4232.  
  4233. consult:  predicates  that  were being traced continue to have trace points
  4234.  
  4235. set after consulting.
  4236.  
  4237.  
  4238.      The tracing facilities of SB-Prolog are in many ways very  similar  to
  4239.  
  4240. those  of  C-Prolog.   However,  leashing  is not supported, and only those
  4241.  
  4242. predicates can be traced which have had trace points set  on  them  through
  4243.  
  4244. trace/1.   This  makes  trace/1  and spy/1 very similar: essentially, trace
  4245.  
  4246. amounts to two levels of spy points.  In SB-Prolog, tracing occurs at  Call
  4247.  
  4248. (i.e.  entry to a predicate), successful Exit from a clause, and Failure of
  4249.  
  4250. the entire call.  The tracing options available during  debugging  are  the
  4251.  
  4252. following:
  4253.  
  4254.  
  4255. c, NEWLINE: Creep
  4256.  
  4257.      Causes the system to single-step to the next  port  (i.e.  either  the
  4258.  
  4259.      entry to a traced predicate called by the executed clause, or the suc-
  4260.  
  4261.      cess or failure exit from that clause).
  4262.  
  4263.  
  4264. a: Abort
  4265.  
  4266.      Causes execution to abort and control  to  return  to  the  top  level
  4267.  
  4268.      interpreter.
  4269.  
  4270.  
  4271. b: Break
  4272.  
  4273.      Calls the evaluable predicate break, thus invoking recursively  a  new
  4274.  
  4275.      incarnation  of  the  system interpreter.  The command prompt at break
  4276.  
  4277.      level n is
  4278.  
  4279.                  n: ?-
  4280.  
  4281.  
  4282.      The user may return to the previous break level by entering the system
  4283.  
  4284.  
  4285.  
  4286.                                     63
  4287.  
  4288.  
  4289.  
  4290.  
  4291.  
  4292.  
  4293.  
  4294.  
  4295.  
  4296.  
  4297.  
  4298.  
  4299.      end-of-file   character   (e.g.   ctrl-D),   or  typing  in  the  atom
  4300.  
  4301.      end_of_file; or to the top level interpreter by typing in abort.
  4302.  
  4303.  
  4304. f: Fail
  4305.  
  4306.      Causes execution to fail, thus transferring control to the  Fail  port
  4307.  
  4308.      of the current execution.
  4309.  
  4310.  
  4311. h: Help
  4312.  
  4313.      Displays the table of debugging options.
  4314.  
  4315.  
  4316. l: Leap
  4317.  
  4318.      Causes the system to resume running the program, only stopping when  a
  4319.  
  4320.      spy-point  is reached or the program terminates.  This allows the user
  4321.  
  4322.      to follow the execution at a higher level than exhaustive tracing.
  4323.  
  4324.  
  4325. n: Nodebug
  4326.  
  4327.      Turns off debug mode.
  4328.  
  4329.  
  4330. q: Quasi-skip
  4331.  
  4332.      This is like Skip except that it does not mask out spy points.
  4333.  
  4334.  
  4335. r: Retry (fail)
  4336.  
  4337.      Transfers to the Call port of the current goal.  Note,  however,  that
  4338.  
  4339.      side effects, such as database modifications etc., are not undone.
  4340.  
  4341.  
  4342. s: Skip
  4343.  
  4344.      Causes tracing to be turned off for the entire execution of  the  pro-
  4345.  
  4346.      cedure.   Thus,  nothing is seen until control comes back to that pro-
  4347.  
  4348.      cedure, either at the Success or the Failure port.
  4349.  
  4350.  
  4351.  
  4352.                                     64
  4353.  
  4354.  
  4355.  
  4356.  
  4357.  
  4358.  
  4359.  
  4360.  
  4361.  
  4362.  
  4363.  
  4364.  
  4365. Other predicates that are useful in debugging are:
  4366.  
  4367.  
  4368.  
  4369.  
  4370. untrace(Preds)
  4371.  
  4372.      where Preds is a term P/N, where P is  a  predicate  name  and  N  its
  4373.  
  4374.      arity,  or  a  list of such terms.  Turns off tracing on the specified
  4375.  
  4376.      predicates.  Preds must be instantiated at the time of the call.
  4377.  
  4378.  
  4379. spy(Preds)
  4380.  
  4381.      where Preds is a term P/N, where P is  a  predicate  name  and  N  its
  4382.  
  4383.      arity,  or  a  list  of  such terms.  Sets spy points on the specified
  4384.  
  4385.      predicates.  Preds must be instantiated at the time of the call.
  4386.  
  4387.  
  4388. nospy(Preds)
  4389.  
  4390.      where Preds is a term P/N, where P is  a  predicate  name  and  N  its
  4391.  
  4392.      arity,  or  a list of such terms.  Removes spy points on the specified
  4393.  
  4394.      predicates.  Preds must be instantiated at the time of the call.
  4395.  
  4396.  
  4397. debug
  4398.  
  4399.      Turns on debugging mode.  This causes subsequent execution  of  predi-
  4400.  
  4401.      cates  with  trace or spy points to be traced, and is a no-op if there
  4402.  
  4403.      are no such predicates.  The predicates trace/1 and spy/1 cause debug-
  4404.  
  4405.      ging mode to be turned on automatically.
  4406.  
  4407.  
  4408. nodebug
  4409.  
  4410.      Turns off debugging mode.  This causes trace  and  spy  points  to  be
  4411.  
  4412.      ignored.
  4413.  
  4414.  
  4415.  
  4416.  
  4417.  
  4418.                                     65
  4419.  
  4420.  
  4421.  
  4422.  
  4423.  
  4424.  
  4425.  
  4426.  
  4427.  
  4428.  
  4429.  
  4430.  
  4431. debugging
  4432.  
  4433.      Displays information about whether debug mode is on or not, and  lists
  4434.  
  4435.      predicates that have trace points or spy points set on them.
  4436.  
  4437.  
  4438. tracepreds(L)
  4439.  
  4440.      Binds L to a list of terms P/N where the predicate P of arity N has  a
  4441.  
  4442.      trace point set on it.
  4443.  
  4444.  
  4445. spypreds(L)
  4446.  
  4447.      Binds L to a list of terms P/N where the predicate P of arity N has  a
  4448.  
  4449.      spy point set on it.
  4450.  
  4451.  
  4452.  
  4453.  
  4454.      There is one known bug in the package: attempts to set  trace  points,
  4455.  
  4456. via  trace/1,  on  system and library predicates that are used by the trace
  4457.  
  4458. package can cause bizarre behaviour.
  4459.  
  4460.  
  4461. 6.2.  Low Level Tracing
  4462.  
  4463.  
  4464.  
  4465.  
  4466.      SB-Prolog also provides a facility for low level tracing of execution.
  4467.  
  4468. This  can  be  activated  by  invoking the simulator with the -T option, or
  4469.  
  4470. through the predicate trace/0.  It causes trace information to  be  printed
  4471.  
  4472. out at every call (including those to system trap handlers).  The volume of
  4473.  
  4474. such trace information can very become large very quickly, so  this  method
  4475.  
  4476. of tracing is not recommended in general.
  4477.  
  4478.  
  4479.      Low level tracing may be turned off using the predicate untrace/0.
  4480.  
  4481.  
  4482.  
  4483.  
  4484.                                     66
  4485.  
  4486.  
  4487.  
  4488.  
  4489.  
  4490.  
  4491.  
  4492.  
  4493.  
  4494.  
  4495.  
  4496.  
  4497. 7.  The Simulator
  4498.  
  4499.  
  4500.  
  4501.  
  4502.      The simulator resides in the SB-Prolog system directory sim.  The fol-
  4503.  
  4504. lowing sections describe various aspects of the simulator.
  4505.  
  4506.  
  4507. 7.1.  Invoking the Simulator
  4508.  
  4509.  
  4510.  
  4511.  
  4512.      The simulator is invoked by the command
  4513.  
  4514.  
  4515.  
  4516.             sbprolog bc_file
  4517.  
  4518.  
  4519.  
  4520.  
  4521. where bc_file is a byte code file resulting from the compilation of a  Pro-
  4522.  
  4523. log  program.  In almost all cases, the user will wish to interact with the
  4524.  
  4525. SB-Prolog query evaluator, in which case bc_file will be $readloop, and the
  4526.  
  4527. command will be
  4528.  
  4529.  
  4530.  
  4531.             sbprolog PATH/\\$eadloop
  4532.  
  4533.  
  4534.  
  4535.  
  4536. where PATH is the path to the directory containing the command  interpreter
  4537.  
  4538. $readloop.  This directory, typically, is the system directory modlib.
  4539.  
  4540.  
  4541.      The command interpreter reads in a query typed in by the user,  evalu-
  4542.  
  4543. ates  it  and  prints  the answer(s), repeating this until it encounters an
  4544.  
  4545. end-of-file (the standard end-of-file character on the system,  e.g.  ctrl-
  4546.  
  4547. D), or the user types in end_of_file or halt.
  4548.  
  4549.  
  4550.                                     67
  4551.  
  4552.  
  4553.  
  4554.  
  4555.  
  4556.  
  4557.  
  4558.  
  4559.  
  4560.  
  4561.  
  4562.  
  4563.      The user should ensure that the the directory containing  the  execut-
  4564.  
  4565. able  file  sim  (typically,  the  system directory sim) is included in the
  4566.  
  4567. shell variable path; if not, the full path to the simulator will have to be
  4568.  
  4569. specified.
  4570.  
  4571.  
  4572.      In general, the simulator may be invoked with a variety of options, as
  4573.  
  4574. follows:
  4575.  
  4576.  
  4577.  
  4578.             sbprolog -options bc_file
  4579.     or
  4580.             sbprolog -option<1> -option<2> ... -option<nbc_file
  4581.  
  4582.  
  4583.  
  4584.  
  4585. The options recognized by the simulator are described below.
  4586.  
  4587.  
  4588.      When called with a byte code file bc_file, the simulator begins execu-
  4589.  
  4590. tion  with the first clause in that file.  The first clause in such a file,
  4591.  
  4592. therefore, should be a clause without any arguments in the head (otherwise,
  4593.  
  4594. the  simulator  will  attempt  to dereference argument pointers in the head
  4595.  
  4596. that are really pointing into deep space, and usually come to a  sad  end).
  4597.  
  4598. If  the  user is executing a file in this manner rather than using the com-
  4599.  
  4600. mand interpreter, he should also be careful to include the undefined predi-
  4601.  
  4602. cate  handler  `_$undefined_pred'/1,  which is normally defined in the file
  4603.  
  4604. modlib/$init_sys.P.
  4605.  
  4606.  
  4607. 7.2.  Simulator Options
  4608.  
  4609.  
  4610.  
  4611.  
  4612.  
  4613.  
  4614.  
  4615.  
  4616.                                     68
  4617.  
  4618.  
  4619.  
  4620.  
  4621.  
  4622.  
  4623.  
  4624.  
  4625.  
  4626.  
  4627.  
  4628.  
  4629. The following is a list of options recognized by the simulator.
  4630.  
  4631.  
  4632. T    Generates a trace at entry to each called routine.
  4633.  
  4634.  
  4635. d    Produces a disassembled dump of bc_file into a file  named  `dump.pil'
  4636.  
  4637.      and exits.
  4638.  
  4639.  
  4640. n    Adds machine addresses when producing trace and dump.
  4641.  
  4642.  
  4643. s    Maintains information for the builtin statistics/0.  Default: off.
  4644.  
  4645.  
  4646. size
  4647.  
  4648.      Allocates size words (4 bytes) of space to the local  stack  and  heap
  4649.  
  4650.      together.  Default: 100000.
  4651.  
  4652.  
  4653. size
  4654.  
  4655.      Allocates size words of space to the program area.  Default: 100000.
  4656.  
  4657.  
  4658. size
  4659.  
  4660.      Allocates size words of space to the trail stack.  Default: m/5, where
  4661.  
  4662.      m  is  the  amount  of  space  allocated  to  the local stack and heap
  4663.  
  4664.      together.  This parameter, if specified, must follow the -m parameter.
  4665.  
  4666.  
  4667.  
  4668.  
  4669. As an example, the command
  4670.  
  4671.             sim -s -p 60000 -m 150000 \$readloop
  4672.  
  4673.  
  4674. starts the simulator executing the command interpreter with 60000 bytes  of
  4675.  
  4676. program  space,  150000  bytes  of  local  and  global  stack space and (by
  4677.  
  4678. default) 30000 bytes of trail stack space; the s  option  also  results  in
  4679.  
  4680.  
  4681.  
  4682.                                     69
  4683.  
  4684.  
  4685.  
  4686.  
  4687.  
  4688.  
  4689.  
  4690.  
  4691.  
  4692.  
  4693.  
  4694.  
  4695. statistics information being maintained.
  4696.  
  4697.  
  4698. 7.3.  Interrupt Handling
  4699.  
  4700.  
  4701.  
  4702.  
  4703.      SB-Prolog provides a  facility  for  exception  handling  using  user-
  4704.  
  4705. definable  interrupt  handlers.   This can be used both for external inter-
  4706.  
  4707. rupts, e.g. those generated from the keyboard by the user or  from  signals
  4708.  
  4709. other  processes;  or internal traps, e.g. those caused by stack overflows,
  4710.  
  4711. encountering undefined  predicates,  etc.   For  example,  the  ``undefined
  4712.  
  4713. predicate''   interrupt   is   handled,   by   default,  by  the  predicate
  4714.  
  4715. `_$undefined_pred'/1, which is defined in the file modlib/$init_sys.P.  The
  4716.  
  4717. default  action  on  encountering  an  undefined predicate is to attempt to
  4718.  
  4719. dynamically load a file whose name matches that of the undefined predicate.
  4720.  
  4721. However,  the  user may easily alter this behaviour by redefining the unde-
  4722.  
  4723. fined predicate handler.
  4724.  
  4725.  
  4726. 8.  The Compiler
  4727.  
  4728.  
  4729.  
  4730.  
  4731. The compiler translates Prolog source files into  byte-code  object  files.
  4732.  
  4733. It  is  written  entirely in Prolog.  The byte code for the compiler can be
  4734.  
  4735. found in the SB-Prolog  system  directory  cmplib,  with  the  source  code
  4736.  
  4737. resident in cmplib/src.
  4738.  
  4739.  
  4740.      Byte code files may be concatenated together  to  produce  other  byte
  4741.  
  4742. code  files.   Thus,  for  example,  if  foo1  and foo2 are byte code files
  4743.  
  4744. resulting from the compilation of two Prolog source programs, then the file
  4745.  
  4746.  
  4747.  
  4748.                                     70
  4749.  
  4750.  
  4751.  
  4752.  
  4753.  
  4754.  
  4755.  
  4756.  
  4757.  
  4758.  
  4759.  
  4760.  
  4761. foo, obtained by executing the shell command
  4762.  
  4763.             cat foo1 foo2 > foo
  4764.  
  4765.  
  4766. is a byte code file as well, and may be loaded and executed.  In this case,
  4767.  
  4768. loading  and  executing  the file foo would give the same result as loading
  4769.  
  4770. foo1 and foo2 separately, which in turn would be the same as  concatenating
  4771.  
  4772. the  original  source  files and compiling this larger file.  This makes it
  4773.  
  4774. easier to compile large programs: one need only  break  them  into  smaller
  4775.  
  4776. pieces,  compile  the  individual  pieces,  and  concatenate the byte files
  4777.  
  4778. together.
  4779.  
  4780.  
  4781.      The following sections describe the various aspects of the compiler in
  4782.  
  4783. more detail.
  4784.  
  4785.  
  4786. 8.1.  Structure of the Compiler
  4787.  
  4788.  
  4789.  
  4790.  
  4791. The compilation of a Prolog program proceeds in four phases.  In the  first
  4792.  
  4793. phase,  the  source  program is read in and  passed through a preprocessor.
  4794.  
  4795. The output of the preprocessor is essentially another  Prolog  source  pro-
  4796.  
  4797. gram.   Next,  this  program is transformed into an internal representation
  4798.  
  4799. (essentially an annotated abstract syntax tree).  The third phase  involves
  4800.  
  4801. generating  WAM  ``assembly'' code and peephole optimization.  Finally, the
  4802.  
  4803. WAM assembly code is processed by an assembler which generates byte code.
  4804.  
  4805.  
  4806. 8.2.  Invoking the Compiler
  4807.  
  4808.  
  4809.  
  4810.  
  4811.  
  4812.  
  4813.  
  4814.                                     71
  4815.  
  4816.  
  4817.  
  4818.  
  4819.  
  4820.  
  4821.  
  4822.  
  4823.  
  4824.  
  4825.  
  4826.  
  4827. The compiler is invoked through the Prolog predicate compile:
  4828.  
  4829.             | ?- compile(InFile [, OutFile ] [, OptionsList ]).
  4830.  
  4831.  
  4832. where optional parameters are enclosed in brackets.  InFile is the name  of
  4833.  
  4834. the  input (i.e. source) file; OutFile is the name of the output file (i.e.
  4835.  
  4836. byte code) file; OptionsList is a list of compiler options (see below).
  4837.  
  4838.  
  4839.      The input and output file names must  be  Prolog  atoms,  i.e.  either
  4840.  
  4841. begin  with  a  lower  case  letter or dollar sign `$', and consist only of
  4842.  
  4843. letters, digits, and underscores; or, be enclosed within single quotes.  If
  4844.  
  4845. the output file name is not specified, it defaults to InFile.out.  The list
  4846.  
  4847. of options, if specified, is a Prolog list, i.e. a term of the form
  4848.  
  4849.             [ option<1>, option<2>, ..., option<n> ].
  4850.  
  4851.  
  4852. If left unspecified, it defaults to the empty list [].
  4853.  
  4854.  
  4855.      In fact, the output file name and the options list may be specified in
  4856.  
  4857. any order.  Thus, for example, the queries
  4858.  
  4859.             | ?- compile('/usr/debray/foo', foo_out, [v]).
  4860.     and
  4861.             | ?- compile('/usr/debray/foo', [v], foo_out).
  4862.  
  4863.  
  4864. are equivalent, and specify that the Prolog source  file  `/usr/debray/foo'
  4865.  
  4866. is  to  be  compiled  in verbose mode (see ``Compiler Options'' below), and
  4867.  
  4868. that the byte code is to be generated into the file foo_out.
  4869.  
  4870.  
  4871.      The compile predicate may also be called with a fourth parameter:
  4872.  
  4873.             | ?- compile(InFileOutFileOptionsListPredList).
  4874.  
  4875.  
  4876. where InFileOutFile and OptionsList  are  as  before;  compile/4  unifies
  4877.  
  4878.  
  4879.  
  4880.                                     72
  4881.  
  4882.  
  4883.  
  4884.  
  4885.  
  4886.  
  4887.  
  4888.  
  4889.  
  4890.  
  4891.  
  4892.  
  4893. PredList  with  a  list  of  terms  P/N  denoting the predicates defined in
  4894.  
  4895. InFile, where P is a predicate name and N its arity.  PredList,  if  speci-
  4896.  
  4897. fied,  is usually given as an uninstantiated variable; its principal use is
  4898.  
  4899. for setting trace points on the predicates in the  file  (see  Section  6),
  4900.  
  4901. e.g. by executing
  4902.  
  4903.             | ?- compile('/usr/debray/foo', foo_out, [v], L), load(foo_out), trace(L).
  4904.  
  4905.  
  4906. Notice that PredList can only appear in compile/4.
  4907.  
  4908.  
  4909. 8.3.  Compiler Options
  4910.  
  4911.  
  4912.  
  4913.  
  4914. The following options are currently recognized by the compiler:
  4915.  
  4916.  
  4917. a    Specifies that an ``assembler'' file is to be created.   The  name  of
  4918.  
  4919.      the  assembler  file  is  obtained by appending ``.asl'' to the source
  4920.  
  4921.      file name.  While the writing out of assembly code slows down the com-
  4922.  
  4923.      pilation  process  to  some  extent,  it  allows the assembler to do a
  4924.  
  4925.      better job of optimizing away indirect subroutine linkages  (since  in
  4926.  
  4927.      this  case  the  assembler has assembly code for the entire program to
  4928.  
  4929.      work with at once, not just a single predicate).  This results in code
  4930.  
  4931.      that is faster and more compact.
  4932.  
  4933.  
  4934. d    Dumps expanded macros to the user (see Section 10).
  4935.  
  4936.  
  4937. e    Expand macros (see Section 10).
  4938.  
  4939.  
  4940. i    Specifies that indexes are to be generated.  The pragma file (see Sec-
  4941.  
  4942.      tion 14) corresponding to the source file is consulted for information
  4943.  
  4944.  
  4945.  
  4946.                                     73
  4947.  
  4948.  
  4949.  
  4950.  
  4951.  
  4952.  
  4953.  
  4954.  
  4955.  
  4956.  
  4957.  
  4958.  
  4959.      regarding predicates which should have indexes  constructed,  and  the
  4960.  
  4961.      arguments on which indexes are to be constructed.
  4962.  
  4963.  
  4964. t    If  specified,  turns  off  assembler  optimizations  that   eliminate
  4965.  
  4966.      indirect  branches  through  the  symbol  table  in  favour  of direct
  4967.  
  4968.      branches.  This is useful in debugging compiled code.  It is necessary
  4969.  
  4970.      if the extension table feature is to be used.
  4971.  
  4972.  
  4973. v    If specified, compiles in  ``verbose''  mode,  which  causes  messages
  4974.  
  4975.      regarding progress of compilation to be printed out.
  4976.  
  4977.  
  4978. 8.4.  A Note on Coding for Efficiency
  4979.  
  4980.  
  4981.  
  4982.  
  4983.      The SB-Prolog system tends to  favour  programs  that  are  relatively
  4984.  
  4985. pure.   Thus,  for example, asserts tend to be quite expensive, encouraging
  4986.  
  4987. the user to avoid them if possible.  This section points out some syntactic
  4988.  
  4989. constructs  that  lead  to the generation of efficient code.  These involve
  4990.  
  4991. (i) avoiding the creation of backtrack points;  and  (ii)  minimizing  data
  4992.  
  4993. movement  between  registers.  Optimization of logic programs is an area of
  4994.  
  4995. ongoing research, and we expect to enhance the capabilities of  the  system
  4996.  
  4997. further in future versions.
  4998.  
  4999.  
  5000. 8.4.1.  Avoiding Creation of Backtrack Points
  5001.  
  5002.  
  5003.  
  5004.  
  5005.      Since the creation of backtrack points is relatively  expensive,  pro-
  5006.  
  5007. gram  efficiency  may  be  improved  substantially by using constructs that
  5008.  
  5009. avoid the creation of  backtrack  points  where  possible.   The  SB-Prolog
  5010.  
  5011.  
  5012.                                     74
  5013.  
  5014.  
  5015.  
  5016.  
  5017.  
  5018.  
  5019.  
  5020.  
  5021.  
  5022.  
  5023.  
  5024.  
  5025. compiler  recognizes  conditionals  involving  certain complementary inline
  5026.  
  5027. tests, and generates code that does  not  create  choice  points  for  such
  5028.                                _        _
  5029. cases.   Two  inline  tests  p(X) and q(X) are complementary if and only if
  5030.   _          _
  5031. p(X) = not(q(X)).  For example, the literals `X > Y' and `X =< Y' are  com-
  5032.  
  5033. plementary.  At this point, complementary tests are recognized as such only
  5034.  
  5035. if their argument tuples are identical.  The  inline  predicates  that  are
  5036.  
  5037. treated  in  this  manner, with their corresponding complementary literals,
  5038.  
  5039. are shown in Table 3.  The syntactic constructs recognized are:
  5040.  
  5041.  
  5042. (i)  Disjuncts of the form
  5043.  
  5044.                          _                      _
  5045.          head :- (( test(X), ... ) ; ( not(test(X)), ...)).
  5046.  
  5047.  
  5048.      or
  5049.  
  5050.  
  5051.  
  5052.                  _________________________________________
  5053.                 |    Inline Test    |  Complementary Test |
  5054.                 |___________________|_____________________|
  5055.                 | >/2               |  =</2               |
  5056.                 |___________________|_____________________|
  5057.                 | =</2              |  >/2                |
  5058.                 |___________________|_____________________|
  5059.                 | >=/2              |  </2                |
  5060.                 |___________________|_____________________|
  5061.                 | </2               |  >=/2               |
  5062.                 |___________________|_____________________|
  5063.                 | =:=/2             |  =\=/2              |
  5064.                 |___________________|_____________________|
  5065.                 | =\=/2             |  =:=/2              |
  5066.                 |___________________|_____________________|
  5067.                 | var/1             |  nonvar/1           |
  5068.                 |___________________|_____________________|
  5069.                 | nonvar/1          |  var/1              |
  5070.                 |___________________|_____________________|
  5071.  
  5072.           Table 3: Complementary tests recognized by the compiler
  5073.  
  5074.  
  5075.  
  5076.  
  5077.  
  5078.                                     75
  5079.  
  5080.  
  5081.  
  5082.  
  5083.  
  5084.  
  5085.  
  5086.  
  5087.  
  5088.  
  5089.                          _                       _
  5090.          head :- (( test(X), ... ) ; ( comp_test(X), ...)).
  5091.  
  5092.  
  5093.      where test is one  of  the  inline  tests  in  the  table  above,  and
  5094.  
  5095.      comp_test  the  corresponding  complementary test (note that the argu-
  5096.  
  5097.      ments to test and comp_test have to be identical).
  5098.  
  5099.  
  5100. (ii) Conditionals of the form
  5101.  
  5102.          head :- (test<1>, ... , test<n>) -> True_Case ; False_Case.
  5103.  
  5104.  
  5105.      or
  5106.  
  5107.          head :- (test<1>; ... ; test<n>) -> True_Case ; False_Case.
  5108.  
  5109.  
  5110.      where each test<i> is an inline test, as mentioned in the table above.
  5111.  
  5112.  
  5113.      The code generated for these cases involves  a  test  and  conditional
  5114.  
  5115. branch,  and  no choice point is created.  We expect future versions of the
  5116.  
  5117. translator to recognize a wider class of complementary tests.
  5118.  
  5119.  
  5120.      Notice that this discourages the use of explicit cuts.   For  example,
  5121.  
  5122. whereas a choice point will be created for
  5123.  
  5124.     part(M,[E|L],U1,U2) :-
  5125.        ((E =< M, !, U1 = [E|U1a], U2 = U2a) ;
  5126.         (U1 = U1a, U2 = [E|U2a])),
  5127.        part(M,L,U1a,U2a).
  5128.  
  5129.  
  5130. no choice point will be created for either
  5131.  
  5132.     part(M,[E|L],U1,U2) :-
  5133.        (E =< M ->
  5134.           (U1 = [E|U1a], U2 = U2a) ;
  5135.           (U1 = U1a, U2 = [E|U2a])),
  5136.        part(M,L,U1a,U2a).
  5137.  
  5138.  
  5139. or
  5140.  
  5141.  
  5142.  
  5143.  
  5144.                                     76
  5145.  
  5146.  
  5147.  
  5148.  
  5149.  
  5150.  
  5151.  
  5152.  
  5153.  
  5154.  
  5155.  
  5156.     part(M,[E|L],U1,U2) :-
  5157.        ((E =< M, U1 = [E|U1a], U2 = U2a) ;
  5158.         (E > M,  U1 = U1a, U2 = [E|U2a])),
  5159.        part(M,L,U1a,U2a).
  5160.  
  5161.  
  5162. Thus, either of the two later versions will be more efficient than the ver-
  5163.  
  5164. sion with the explicit cut.
  5165.  
  5166.  
  5167. 8.4.2.  Minimizing Data Movement Between Registers
  5168.  
  5169.  
  5170.  
  5171.  
  5172.      Data movement between registers for parameter passing may be minimized
  5173.  
  5174. by  leaving  variables  in  the  same  argument position wherever possible.
  5175.  
  5176. Thus, the clause
  5177.  
  5178.             p(X,Y) :- p1(X,Y,0).
  5179.  
  5180.  
  5181. is preferable to
  5182.  
  5183.             p(X,Y) :- p1(0,X,Y).
  5184.  
  5185.  
  5186. because the first definition leaves the variables X and Y in the same argu-
  5187.  
  5188. ment  positions  (first and second, respectively), while the second defini-
  5189.  
  5190. tion does not.
  5191.  
  5192.  
  5193. 8.5.  Assembly
  5194.  
  5195.  
  5196.  
  5197.  
  5198.      The SB-Prolog assembler can be invoked by  loading  the  compiler  and
  5199.  
  5200. using the predicate $asm/3:
  5201.  
  5202.             | ?- $asm(InFileOutFileOptionsList).
  5203.  
  5204.  
  5205. where InFile is a Prolog atom which is the name of a  WAM  assembly  source
  5206.  
  5207. file  (e.g.   the ``.asl'' file generated when a Prolog program is compiled
  5208.  
  5209.  
  5210.                                     77
  5211.  
  5212.  
  5213.  
  5214.  
  5215.  
  5216.  
  5217.  
  5218.  
  5219.  
  5220.  
  5221.  
  5222.  
  5223. with the ``a'' option), OutFile is  an  atom  which  is  the  name  of  the
  5224.  
  5225. intended byte code file, and OptionsList is a list of options.  The options
  5226.  
  5227. recognized by the assembler are:
  5228.  
  5229.  
  5230. v    ``Verbose'' mode.  Prints out information regarding progress of assem-
  5231.  
  5232.      bly.
  5233.  
  5234.  
  5235. t    ``Trace''.  If specified, the assembler generates code to  force  pro-
  5236.  
  5237.      cedure  calls  to  branch  indirectly via the symbol table, instead of
  5238.  
  5239.      using a direct branch.  This is Useful for tracing compiled code.   It
  5240.  
  5241.      is necessary if the extension table feature is to be used.
  5242.  
  5243.  
  5244. The assembler is intended primarily to support the compiler, so the  assem-
  5245.  
  5246. bly  language syntax is quirky in places.  The interested reader is advised
  5247.  
  5248. to look at the assembly files resulting from  compilation  with  the  ``a''
  5249.  
  5250. option for more on SB-Prolog assembler syntax.
  5251.  
  5252.  
  5253. 9.  Modules
  5254.  
  5255.  
  5256.  
  5257.  
  5258.      To describe how modules (or maybe more properly, just  libraries)  are
  5259.  
  5260. currently  supported in our system, we must describe the _$undefined_pred/1
  5261.  
  5262. interrupt handler.  The system keeps a table of modules and  routines  that
  5263.  
  5264. are needed from each.  When a predicate is found to be undefined, the table
  5265.  
  5266. is searched to see if it is defined by some module.  If so, that module  is
  5267.  
  5268. loaded  (if  it  hasn't been previously loaded) and the association is made
  5269.  
  5270. between the routine name as defined in the module, and the routine name  as
  5271.  
  5272. used by the invoker.
  5273.  
  5274.  
  5275.  
  5276.                                     78
  5277.  
  5278.  
  5279.  
  5280.  
  5281.  
  5282.  
  5283.  
  5284.  
  5285.  
  5286.  
  5287.  
  5288.  
  5289.      The table of modules and needed routines is:
  5290.  
  5291.      defined_mods(Modname, [pred<1>/arity<1>, ..., pred<n>/arity<n>]).
  5292.  
  5293.  
  5294. where Modname is the name of the module.  The module  exports  n  predicate
  5295.  
  5296. definitions.  The first exported pred is of arity arity<>1, and needs to be
  5297.  
  5298. invoked by the name of pred<1>.
  5299.  
  5300.  
  5301.      The table of modules that have already been loaded is given by
  5302.  
  5303.             loaded_mods(Modname).
  5304.  
  5305.  
  5306. A module is a file of predicate definitions, together with a fact  defining
  5307.  
  5308. a  list  of predicates exported by that module, and a set of facts, each of
  5309.  
  5310. which specifies, for some other module, the predicates imported  from  that
  5311.  
  5312. module.   For  example,  consider  a  module name `p'. It contains a single
  5313.  
  5314. fact, named p_export, that is true of the list  of  predicate/arity's  that
  5315.  
  5316. are exported.  E.g.
  5317.  
  5318.             p_export([p1/2, p2/4])
  5319.  
  5320.  
  5321. indicates that the module p exports the predicates p1/2 and p2/4.  For each
  5322.  
  5323. module  m which contains predicates needed by the module p, there is a fact
  5324.  
  5325. for p_use, describing what module is needed and the names of the predicates
  5326.  
  5327. defined  there  that  are needed.  For example, if module p needs to import
  5328.  
  5329. predicates ip1/2 and ip2/3 from module q, there would be a fact
  5330.  
  5331.             p_use(q,[ip1/2, ip2/3])
  5332.  
  5333.  
  5334. where q is a module that exports two predicates: one 2-ary and  one  3-ary.
  5335.  
  5336. This list corresponds to the export list of module q.
  5337.  
  5338.  
  5339.  
  5340.  
  5341.  
  5342.                                     79
  5343.  
  5344.  
  5345.  
  5346.  
  5347.  
  5348.  
  5349.  
  5350.  
  5351.  
  5352.  
  5353.  
  5354.  
  5355.      The correspondence between the predicates in the  export  list  of  an
  5356.  
  5357. exporting  module,  and  those  in the import or use list of a module which
  5358.  
  5359. imports one or more of them, is by position, i.e. the  predicate  names  at
  5360.  
  5361. the  exporting  and  importing  names may be different, and the association
  5362.  
  5363. between names in the two lists is by the position  in  the  list.   If  the
  5364.  
  5365. importing  module  does  not  wish  to import one or more of the predicates
  5366.  
  5367. exported by the exporting module, it may put an anonymous variable  in  the
  5368.  
  5369. corresponding  position  in  its  use list.  Thus, for example, if module p
  5370.  
  5371. above had wished to import only the predicate  ip2/3  from  module  q,  the
  5372.  
  5373. corresponding use fact would be
  5374.  
  5375.             p_use(q, [_, ip2/3]).
  5376.  
  5377.  
  5378.  
  5379.      The initial set of predicates and the modules from which they  are  to
  5380.  
  5381. be  loaded is set up by an initial call to $prorc/0 (see the SB-Prolog sys-
  5382.  
  5383. tem file modlib/src/$prorc.P).  This predicate makes initial calls  to  the
  5384.  
  5385. predicate  $define_mod  which set up the tables described above so that the
  5386.  
  5387. use of standard predicates will cause the correct modules to be  loaded  in
  5388.  
  5389. the _$undefined_pred routine, and the correct names to be used.
  5390.  
  5391.  
  5392. 10.  Macros
  5393.  
  5394.  
  5395.  
  5396.  
  5397.      SB-Prolog features a facility for the definition and expansion of mac-
  5398.  
  5399. ros  that is fully compatible with the runtime system.  Its basic mechanism
  5400.  
  5401. is a simple partial evaluator.  It is called by both consult  and  compile,
  5402.  
  5403. so  that macro expansion occurs independently of whether the code is inter-
  5404.  
  5405. preted  or  compiled  (but  not  when  asserted).   Moreover,   the   macro
  5406.  
  5407.  
  5408.                                     80
  5409.  
  5410.  
  5411.  
  5412.  
  5413.  
  5414.  
  5415.  
  5416.  
  5417.  
  5418.  
  5419.  
  5420.  
  5421. definitions av%retained as clauses at runtime, so that invocation of macros
  5422.  
  5423. via call/1 at runtime (or from asserted clauses) does not pose  a  problem.
  5424.  
  5425. This  means,  however,  that  if  the  same macro is used in many different
  5426.  
  5427. files, it will be loaded more than once,  thus  leading  to  wasetd  space.
  5428.  
  5429. This ought to be thought about and fixed.
  5430.  
  5431.  
  5432.      The source for the macro expander is  in  the  SB-Prolog  system  file
  5433.  
  5434. modlib/src/$mac.P.
  5435.  
  5436.  
  5437. 10.1.  Defining Macros
  5438.  
  5439.  
  5440.  
  5441.  
  5442.      `Macros', or predicates to be evaluated at compile-time,  are  defined
  5443.  
  5444. by clauses of the form
  5445.  
  5446.             Head ::- Body
  5447.  
  5448.  
  5449. where facts have `true' as their body.  The partial evaluator  will  expand
  5450.  
  5451. any call to a predicate defined by ::-/2 that unifies with the head of only
  5452.  
  5453. one clause in ::-/2. If a call unifies with  the  head  of  more  than  one
  5454.  
  5455. clause  in  ::-/2, it will not be expanded Notice that this is not a funda-
  5456.  
  5457. mental restriction, since `;' is permitted in the body of  a  clause.   The
  5458.  
  5459. partial evaluator also converts each definition of the form
  5460.  
  5461.             Head ::- Body.
  5462.  
  5463.  
  5464. to a clause of the form
  5465.  
  5466.             Head :- Body.
  5467.  
  5468.  
  5469. and adds this second clause to the other ``normal'' clauses that were  read
  5470.  
  5471. from  the  file.   This  ensures  that  calls to the macro at runtime, e.g.
  5472.  
  5473.  
  5474.                                     81
  5475.  
  5476.  
  5477.  
  5478.  
  5479.  
  5480.  
  5481.  
  5482.  
  5483.  
  5484.  
  5485.  
  5486.  
  5487. through call/1 or from unexpanded calls in the program  do  not  cause  any
  5488.  
  5489. problems.
  5490.  
  5491.  
  5492.      The  partial  evaluator  is  actually  a  Prolog  interpreter  written
  5493.  
  5494. `purely'  in   Prolog,  i.e.,  variable assignments are explicitly handled.
  5495.  
  5496. This is necessary to be able to handle impure constructs such  as  `var(X),
  5497.  
  5498. X=a'.  As a result this is a very slow Prolog evaluator.
  5499.  
  5500.  
  5501.      Since naive partial evaluation can  go  into  an  infinite  loop,  SB-
  5502.  
  5503. Prolog's  partial  evaluator  maintains  a  depth-bound and will not expand
  5504.  
  5505. recursive calls deeper than that.  The depth is determined by the globalset
  5506.  
  5507. predicate  $mac_depth.  The default value for $mac_depth is 50  This can be
  5508.  
  5509. changed to some other value n by executing
  5510.  
  5511.             | ?- globalset($mac_depth(n)).
  5512.  
  5513.  
  5514.  
  5515. 10.2.  Macro Expander Options
  5516.  
  5517.  
  5518.  
  5519.  
  5520. The following options are recognized by the macro expander:
  5521.  
  5522.  
  5523. d    Dumps all clauses to the user after expansion.  Useful for debugging.
  5524.  
  5525.  
  5526. e    Expand macros.  If omitted, the expander simply  converts  each  ::-/2
  5527.  
  5528.      clause to a normal :-/2 clause.
  5529.  
  5530.  
  5531. v    ``Verbose'' mode.  Prints macros that are/are not being expanded.
  5532.  
  5533.  
  5534.  
  5535.  
  5536.  
  5537.  
  5538.  
  5539.  
  5540.                                     82
  5541.  
  5542.  
  5543.  
  5544.  
  5545.  
  5546.  
  5547.  
  5548.  
  5549.  
  5550.  
  5551.  
  5552.  
  5553. 11.  Extension Tables
  5554.  
  5555.  
  5556.  
  5557.  
  5558.      Extension tables store the calls and answers for  a  predicate.  If  a
  5559.  
  5560. call  has  been made before, answers are retrieved from the extension table
  5561.  
  5562. instead of being recomputed. Extension tables provide a  caching  mechanism
  5563.  
  5564. for  Prolog.  In  addition, extension tables affect the termination charac-
  5565.  
  5566. teristics of recursive programs. Some Prolog programs, which are  logically
  5567.  
  5568. correct,  enter  an infinite loop due to recursive predicates. An extension
  5569.  
  5570. table saved on recursive predicates can find all answers (provided the  set
  5571.  
  5572. of  such answers is finite) and terminate for some logic programs for which
  5573.  
  5574. Prolog's evaluation strategy enters an infinite loop. Iterations  over  the
  5575.  
  5576. extension  table execution strategy provides complete evaluation of queries
  5577.  
  5578. over function-free Horn clause programs.
  5579.  
  5580.  
  5581.      To be able to use the simple extension table evaluation on  a  set  of
  5582.  
  5583. predicates,  the  source  file should either be consulted, or compiled with
  5584.  
  5585. the t option (the t option keeps the assembler from  optimizing  subroutine
  5586.  
  5587. linkage  and  allows  the  extension  table  facility to intercept calls to
  5588.  
  5589. predicates).
  5590.  
  5591.  
  5592.      To use extension table execution, all predicates that are to be  saved
  5593.  
  5594. in the extension table must be passed to et/1. For example,
  5595.  
  5596.             | ?- et([pred1/1, pred2/2]), et(pred3/2)
  5597.  
  5598.  
  5599. will set up ``ET-points'' for  the  for  predicates  pred1/1,  pred2/2  and
  5600.  
  5601. pred3/2, which will cause extension tables for these predicates to be main-
  5602.  
  5603. tained during execution. At the time of the call to et/1, these  predicates
  5604.  
  5605.  
  5606.                                     83
  5607.  
  5608.  
  5609.  
  5610.  
  5611.  
  5612.  
  5613.  
  5614.  
  5615.  
  5616.  
  5617.  
  5618.  
  5619. must be defined, either by having been loaded, or through consult.
  5620.  
  5621.  
  5622.      The predicate noet/1 takes a list of predicate/arity pairs  for  which
  5623.  
  5624. ET-points  should be deleted.  Notice that once an ET-point has been set up
  5625.  
  5626. for a predicate, it  will  be  maintained  unless  explicitly  deleted  via
  5627.  
  5628. noet/1.   If the definition of a predicate which has an ET-point defined is
  5629.  
  5630. to be updated, the ET-point must first be deleted via noet/1.   The  predi-
  5631.  
  5632. cate can then be reloaded and a new ET-point established.  This is enforced
  5633.  
  5634. by the failure of the goal ``et(P/N)'' if an ET-point  already  exists  for
  5635.  
  5636. the  argument predicate.  In this case, the following error message will be
  5637.  
  5638. displayed:
  5639.  
  5640.             *et* already defined for: P/N
  5641.  
  5642.  
  5643.  
  5644.      There are, in fact, two extension  table  algorithms:  a  simple  one,
  5645.  
  5646. which simply caches calls to predicates which have ET-points defined; and a
  5647.  
  5648. complete ET algorithm, which iterates the simple extension table  algorithm
  5649.  
  5650. until no more answers can be found.  The simple algorithm is more efficient
  5651.  
  5652. than the complete one; however, the simple algorithm is  not  complete  for
  5653.  
  5654. certain  especially  nasty  forms  of  mutual recursion, while the complete
  5655.  
  5656. algorithm is.  To use the simple extension table algorithm, predicates  can
  5657.  
  5658. simply  be  called as usual.  The complete extension table algorithm may be
  5659.  
  5660. used via the query
  5661.  
  5662.             | ?- et_star(Query).
  5663.  
  5664.  
  5665.  
  5666. The extension table algorithm is intended for predicates that are  ``essen-
  5667.  
  5668. tially  pure'',  and results are not guaranteed for code using impure code.
  5669.  
  5670.  
  5671.  
  5672.                                     84
  5673.  
  5674.  
  5675.  
  5676.  
  5677.  
  5678.  
  5679.  
  5680.  
  5681.  
  5682.  
  5683.  
  5684.  
  5685. The extension table algorithm  saves  only  those  answers  which  are  not
  5686.  
  5687. instances  of  what  is already in the table, and uses these answers if the
  5688.  
  5689. current call is an instance of a call already made.  For example, if a call
  5690.  
  5691. p(X,  Y), with X and Y uninstantiated, is encountered and inserted into the
  5692.  
  5693. extension table, then a subsequent call p(Xb) will be computed using  the
  5694.  
  5695. answers for p(XY) already in the extension table.  Notice that this might
  5696.  
  5697. not work if var/nonvar tests are used on the second argument in the evalua-
  5698.  
  5699. tion of p.
  5700.  
  5701.  
  5702.      Another problem with using impure code is that if an ET  predicate  is
  5703.  
  5704. cut  over,  then the saved call implies that all answers for that predicate
  5705.  
  5706. were computed, but there are only partial results in the ET because of  the
  5707.  
  5708. cut.   So  on  a subsequent call the incomplete extension table answers are
  5709.  
  5710. used when all answers are expected.
  5711.  
  5712.     Example:
  5713.             r(X,Y) :- p(X,Y),q(Y,Z),!,fail.
  5714.             | ?-  r(X,Y) ; p(X,Y).
  5715.  
  5716.  
  5717.  
  5718. Let p be an ET predicate whose  evaluation  yields  many  tuples.   In  the
  5719.  
  5720. evaluation  of  the  query,  r(X,Y)  makes a call to p(X,Y).  Assuming that
  5721.  
  5722. there is a tuple such that q(Y,Z) succeeds with the first p tuple then  the
  5723.  
  5724. evaluation  of  p  is  cut  over.  The call to p(X,Y) in the query uses the
  5725.  
  5726. extension table because of the previous call in the evaluation  of  r(X,Y).
  5727.  
  5728. Only  one  answer is found, whereas the relation p contains many tuples, so
  5729.  
  5730. the computation is not complete.  Note that "cuts" used within the  evalua-
  5731.  
  5732. tion  of an ET predicate are ok, as long as they don't cut over the evalua-
  5733.  
  5734. tion of another ET predicate. The evaluation of  the  predicate  that  uses
  5735.  
  5736.  
  5737.  
  5738.                                     85
  5739.  
  5740.  
  5741.  
  5742.  
  5743.  
  5744.  
  5745.  
  5746.  
  5747.  
  5748.  
  5749.  
  5750.  
  5751. cuts  does  not  cut  over any et processing (such as storing or retrieving
  5752.  
  5753. answers) so that the tuples that are computed are saved. In  the  following
  5754.  
  5755. example,  the ET is used to generate prime numbers where an ET point is put
  5756.  
  5757. on prime/1.
  5758.  
  5759.     Example:
  5760.  
  5761.     prime(I) :- globalset(globalgenint(2)),fail.                    /* Generating Primes */
  5762.     prime(I) :- genint(I), not(div(I)).
  5763.     div(I) :- prime(X), 0 is I mod X.
  5764.  
  5765.     genint(N) :-
  5766.         repeat,
  5767.             globalgenint(N),
  5768.             N1 is N+1,
  5769.             globalset(globalgenint(N1)).
  5770.  
  5771.  
  5772.  
  5773. The following summarizes the library predicates  supporting  the  extension
  5774.  
  5775. table facility:
  5776.  
  5777.  
  5778.  
  5779.  
  5780. et(L)
  5781.  
  5782.  
  5783.      Sets up an ET-point on the predicates L, which causes calls and anwers
  5784.  
  5785.      to  these  predicates  to  be  saved  in an ``extension table''.  L is
  5786.  
  5787.      either a term Pred/Arity, where Pred is a predicate symbol  and  Arity
  5788.  
  5789.      its  arity,  or  a set of such terms represented as a list.  L must be
  5790.  
  5791.      instantiated, and the predicates specified in it defined, at the  time
  5792.  
  5793.      of  the  call  to  et/1.  Gives error messages and fails if any of the
  5794.  
  5795.      predicates in L is undefined, or if an ET-point already exists on  any
  5796.  
  5797.      of  them; in this case, no ET-point is set up on any of the predicates
  5798.  
  5799.      in L.
  5800.  
  5801.  
  5802.  
  5803.  
  5804.                                     86
  5805.  
  5806.  
  5807.  
  5808.  
  5809.  
  5810.  
  5811.  
  5812.  
  5813.  
  5814.  
  5815.  
  5816.  
  5817. et_star(Goal)
  5818.  
  5819.  
  5820.      Invokes the complete extension table algorithm on the goal Goal.
  5821.  
  5822.  
  5823. et_points(L)
  5824.  
  5825.  
  5826.      Unifies L with a list of predicates for which an ET-point is  defined.
  5827.  
  5828.      L is the empty list [] if there are no ET-points defined.
  5829.  
  5830.  
  5831. noet(L)
  5832.  
  5833.  
  5834.      Deletes ET-points on the predicates specified in L.   L  is  either  a
  5835.  
  5836.      term P/N, where P is the name of a predicate and N its arity, or a set
  5837.  
  5838.      of such terms represented as a list.  Gives error messages  and  fails
  5839.  
  5840.      if  there  is  no  ET-point  on  any of the predicates specified in L.
  5841.  
  5842.      Deleting an ET-point for  a  predicate  also  removes  the  calls  and
  5843.  
  5844.      answers  stored in the extension table for that predicate.  The exten-
  5845.  
  5846.      sion tables for all predicates for which ET-points are defined may  be
  5847.  
  5848.      deleted using et_points/1 in cojnunction with noet/1.
  5849.  
  5850.  
  5851.  
  5852.      L must be instantiated at the time of the call to noet/1.
  5853.  
  5854.  
  5855. et_remove(L)
  5856.  
  5857.  
  5858.      Removes both calls and answers for the predicates specified in L.   In
  5859.  
  5860.      effect, this results in the extension table for these predicates to be
  5861.  
  5862.      set to empty.  L must be instantiated at  the  time  of  the  call  to
  5863.  
  5864.      either  a  term P/N, where P is a predicate with arity N, or a list of
  5865.  
  5866.      such terms.  An error occurs if any of the predicates in  L  does  not
  5867.  
  5868.  
  5869.  
  5870.                                     87
  5871.  
  5872.  
  5873.  
  5874.  
  5875.  
  5876.  
  5877.  
  5878.  
  5879.  
  5880.  
  5881.  
  5882.  
  5883.      have an ET-point set.
  5884.  
  5885.  
  5886.  
  5887.      All extension tables can be emptied by using et_points/1  in  conjunc-
  5888.  
  5889.      tion with et_remove/1.
  5890.  
  5891.  
  5892. et_answers(P/NTerm)
  5893.  
  5894.  
  5895.      Retrieves the answers stored in the extension table for the  predicate
  5896.  
  5897.      P/N  in  Term  one at a time.  Term is of the form P(t<1>, ..., t<N>).
  5898.  
  5899.      An error results and et_answers/2 fails if P/N is not fully  specified
  5900.  
  5901.      (ground), or if P/N does not have an ET-point set.
  5902.  
  5903.  
  5904. et_calls(P/NTerm)
  5905.  
  5906.  
  5907.      Retrieves the calls stored in the extension table  for  the  predicate
  5908.  
  5909.      P/N  in  Term  one at a time.  Term is of the form P(t<1>, ..., t<N>).
  5910.  
  5911.      An error results and et_calls/2 fails if P/N is  not  fully  specified
  5912.  
  5913.      (ground), or if P/N does not have an ET-point set.
  5914.  
  5915.  
  5916. 12.  Profiling Programs
  5917.  
  5918.  
  5919.  
  5920.  
  5921.      SB-Prolog provides a utility  for  profiling  programs  interactively.
  5922.  
  5923. Two  kinds of profiling are supported: one may count the number of calls to
  5924.  
  5925. a predicate, or compute the time spent in a  predicate.   It  is  important
  5926.  
  5927. that  the  predicates being profiled are either consulted, or compiled with
  5928.  
  5929. the t option, so that calls to the relevant predicates can  be  intercepted
  5930.  
  5931. by the profiler.
  5932.  
  5933.  
  5934.  
  5935.  
  5936.                                     88
  5937.  
  5938.  
  5939.  
  5940.  
  5941.  
  5942.  
  5943.  
  5944.  
  5945.  
  5946.  
  5947.  
  5948.  
  5949.      To use the profiler, predicates whose calls are to be counted must  be
  5950.  
  5951. passed to count/1, e.g.
  5952.  
  5953.             | ?- count([p/1, q/2]), count(r/3).
  5954.  
  5955.  
  5956. will set up ``count-points'' on the predicates p/1, q/2  and  r/3.   Predi-
  5957.  
  5958. cates whose calls are to be timed have to be passed to time/1, e.g.
  5959.  
  5960.             | ?- time([s/1, t/2]), time(u/3).
  5961.  
  5962.  
  5963. will set up ``time-points'' on the predicates s/1, t/2 and u/3.  It is pos-
  5964.  
  5965. sible  to  set  both  count-points  and  time-points on the same predicate.
  5966.  
  5967. After count-points and time-points have been set, the program may  be  exe-
  5968.  
  5969. cuted  as  many times as desired: the profiling system will accumulate call
  5970.  
  5971. counts and execution times for the appropriate predicates.  Execution  pro-
  5972.  
  5973. files  may  be  obtained using the predicates prof_stats/0 or prof_stats/1.
  5974.  
  5975. Using prof_stats/0 to display the execution profile  will  cause  the  call
  5976.  
  5977. counts  and  execution  times of predicates being profiled to be reset to 0
  5978.  
  5979. (this may be avoided by using prof_stats/1).
  5980.  
  5981.  
  5982.      It should be noted that in this context, the ``execution time'' for  a
  5983.  
  5984. predicate  is  an  estimate  of  the total time spent in the subtrees below
  5985.  
  5986. calls to that predicate (including failed subtrees):  thus,  the  execution
  5987.  
  5988. time figures may be dilated slightly if the subtree below a timed predicate
  5989.  
  5990. contains predicates that are being profiled, because of the time taken  for
  5991.  
  5992. updating the call counts and execution times.  For each predicate, the exe-
  5993.  
  5994. cution time is displayed as the fraction of time spent, in  computation  in
  5995.  
  5996. subtrees  under  calls to that predicate, relative to the time elapsed from
  5997.  
  5998. the last time profiling was timed on or the last time profiling  statistics
  5999.  
  6000.  
  6001.  
  6002.                                     89
  6003.  
  6004.  
  6005.  
  6006.  
  6007.  
  6008.  
  6009.  
  6010.  
  6011.  
  6012.  
  6013.  
  6014.  
  6015. were taken, whichever was more recent.
  6016.  
  6017.  
  6018.      The following summarizes the library predicates supporting profiling:
  6019.  
  6020.  
  6021.  
  6022.  
  6023. count(L)
  6024.  
  6025.  
  6026.      Sets up a count-point on the predicates L, which causes calls to these
  6027.  
  6028.      predicates  to be counted, and turns profiling on.  L is either a term
  6029.  
  6030.      Pred/Arity, where Pred is a predicate symbol and Arity its arity, or a
  6031.  
  6032.      set  of such terms represented as a list.  L must be instantiated, and
  6033.  
  6034.      the predicates specified in it defined, at the time  of  the  call  to
  6035.  
  6036.      count/1.
  6037.  
  6038.  
  6039. time(L)
  6040.  
  6041.  
  6042.      Sets up a time-point on the predicates L, which causes execution times
  6043.  
  6044.      for  calls  to these predicates to be accumulated, and turns profiling
  6045.  
  6046.      on.  L is either a term Pred/Arity, where Pred is a  predicate  symbol
  6047.  
  6048.      and  Arity its arity, or a set of such terms represented as a list.  L
  6049.  
  6050.      must be instantiated, and the predicates specified in it  defined,  at
  6051.  
  6052.      the time of the call to time/1.
  6053.  
  6054.  
  6055. nocount(L)
  6056.  
  6057.  
  6058.      Deletes the count-point on the predicates  L.   L  is  either  a  term
  6059.  
  6060.      Pred/Arity, where Pred is a predicate symbol and Arity its arity, or a
  6061.  
  6062.      set of such terms represented as a list.  L must be instantiated,  and
  6063.  
  6064.      the  predicates  specified  in  it defined, at the time of the call to
  6065.  
  6066.  
  6067.  
  6068.                                     90
  6069.  
  6070.  
  6071.  
  6072.  
  6073.  
  6074.  
  6075.  
  6076.  
  6077.  
  6078.  
  6079.  
  6080.  
  6081.      nocount/1.
  6082.  
  6083.  
  6084. notime(L)
  6085.  
  6086.  
  6087.      Deletes the time-point on the  predicates  L.   L  is  either  a  term
  6088.  
  6089.      Pred/Arity, where Pred is a predicate symbol and Arity its arity, or a
  6090.  
  6091.      set of such terms represented as a list.  L must be instantiated,  and
  6092.  
  6093.      the  predicates  specified  in  it defined, at the time of the call to
  6094.  
  6095.      time/1.
  6096.  
  6097.  
  6098. profiling
  6099.  
  6100.  
  6101.      Displays information about whether profile mode  is  on  or  not,  and
  6102.  
  6103.      lists predicates that have count- and time-points set on them.
  6104.  
  6105.  
  6106. prof_reset(L)
  6107.  
  6108.  
  6109.      Resets call counts and/or execution times for the predicates L.  L  is
  6110.  
  6111.      either  a  term Pred/Arity, where Pred is a predicate symbol and Arity
  6112.  
  6113.      its arity, or a set of such terms represented as a list.   L  must  be
  6114.  
  6115.      instantiated,  and the predicates specified in it defined, at the time
  6116.  
  6117.      of the call to prof_reset/1.
  6118.  
  6119.  
  6120. resetcount(L)
  6121.  
  6122.  
  6123.      Resets call  counts  for  the  predicates  L.   L  is  either  a  term
  6124.  
  6125.      Pred/Arity, where Pred is a predicate symbol and Arity its arity, or a
  6126.  
  6127.      set of such terms represented as a list.  L must be instantiated,  and
  6128.  
  6129.      the  predicates  specified  in  it defined, at the time of the call to
  6130.  
  6131.      resetcount/1.
  6132.  
  6133.  
  6134.                                     91
  6135.  
  6136.  
  6137.  
  6138.  
  6139.  
  6140.  
  6141.  
  6142.  
  6143.  
  6144.  
  6145.  
  6146.  
  6147. resettime(L)
  6148.  
  6149.  
  6150.      Resets execution times for the predicates  L.   L  is  either  a  term
  6151.  
  6152.      Pred/Arity, where Pred is a predicate symbol and Arity its arity, or a
  6153.  
  6154.      set of such terms represented as a list.  L must be instantiated,  and
  6155.  
  6156.      the  predicates  specified  in  it defined, at the time of the call to
  6157.  
  6158.      resettime/1.
  6159.  
  6160.  
  6161. profile
  6162.  
  6163.  
  6164.      Turns profiling on.  This causes subsequent  execution  of  predicates
  6165.  
  6166.      with count- or time-points to be profiled, and is a no-op if there are
  6167.  
  6168.      no such predicates.  The predicates count/1 and time/1 cause profiling
  6169.  
  6170.      to be turned on automatically.
  6171.  
  6172.  
  6173. noprofile
  6174.  
  6175.  
  6176.      Turns profiling  off.   This  causes  count-  and  time-points  to  be
  6177.  
  6178.      ignored.
  6179.  
  6180.  
  6181. timepreds(L)
  6182.  
  6183.  
  6184.      Unifies L to a list of terms P/N where the predicate P of arity N  has
  6185.  
  6186.      a time point set on it.
  6187.  
  6188.  
  6189. countpreds(L)
  6190.  
  6191.  
  6192.      Unifies L to a list of terms P/N where the predicate P of arity N  has
  6193.  
  6194.      a count point set on it.
  6195.  
  6196.  
  6197.  
  6198.  
  6199.  
  6200.                                     92
  6201.  
  6202.  
  6203.  
  6204.  
  6205.  
  6206.  
  6207.  
  6208.  
  6209.  
  6210.  
  6211.  
  6212.  
  6213. prof_stats
  6214.  
  6215.  
  6216.      Causes the call counts and/or execution times  accumulated  since  the
  6217.  
  6218.      last  call  to  prof_stats/0 to be printed out for predicates that are
  6219.  
  6220.      being profiled.  The execution times are given  as  fractions  of  the
  6221.  
  6222.      total time elapsed since the last time profiling was turned on, or the
  6223.  
  6224.      last time prof_stats was called, whichever was most recent.  This also
  6225.  
  6226.      results  in  the  call  counts  and  relative execution times of these
  6227.  
  6228.      predicates being reset to 0.  Equivalent to prof_stats(1).
  6229.  
  6230.  
  6231. prof_stats(N)
  6232.  
  6233.  
  6234.      Causes the call counts and/or execution times  accumulated  since  the
  6235.  
  6236.      last  call  to  prof_stats/0 to be printed out for predicates that are
  6237.  
  6238.      being profiled.  The execution times are given  as  fractions  of  the
  6239.  
  6240.      total time elapsed since the last time profiling was turned on, or the
  6241.  
  6242.      last time prof_stats was called, whichever was most recent.  If  N  is
  6243.  
  6244.      1,  then  this  also results in the call counts and execution times of
  6245.  
  6246.      these predicates being reset to 0; otherwise, the call counts and exe-
  6247.  
  6248.      cution times are not reset.
  6249.  
  6250.  
  6251. 13.  Other Library Utilities
  6252.  
  6253.  
  6254.  
  6255.  
  6256.      The SB-Prolog library contains various other utilities, some of  which
  6257.  
  6258. are listed below.
  6259.  
  6260.  
  6261. $append(XYZ)
  6262.  
  6263.  
  6264.  
  6265.  
  6266.                                     93
  6267.  
  6268.  
  6269.  
  6270.  
  6271.  
  6272.  
  6273.  
  6274.  
  6275.  
  6276.  
  6277.  
  6278.  
  6279.      Succeeds if list Z is the concatenation of lists X and Y.
  6280.  
  6281.  
  6282. $member(XL)
  6283.  
  6284.  
  6285.      Checks whether X unifies with any element of list L,  succeeding  more
  6286.  
  6287.      than once if there are multiple such elements.
  6288.  
  6289.  
  6290. $memberchk(XL)
  6291.  
  6292.  
  6293.      Similar to $member/2, except that $memberchk/2 is deterministic,  i.e.
  6294.  
  6295.      does not succeed more than once for any call.
  6296.  
  6297.  
  6298. $reverse(LR)
  6299.  
  6300.  
  6301.      Succeeds if R is the reverse of list L.  If L is not  a  fully  deter-
  6302.  
  6303.      mined  list,  i.e.  if the tail of L is a variable, this predicate can
  6304.  
  6305.      succeed arbitrarily many times.
  6306.  
  6307.  
  6308. $merge(XYZ)
  6309.  
  6310.  
  6311.      Succeeds if Z is the list resulting from ``merging'' lists  X  and  Y,
  6312.  
  6313.      i.e. the elements of X together with any element of Y not occurring in
  6314.  
  6315.      X.  If X or Y contain duplicates, Z may also contain duplicates.
  6316.  
  6317.  
  6318. $absmember(XL)
  6319.  
  6320.  
  6321.      Similar to $member/2, except that  it  checks  for  identity  (through
  6322.  
  6323.      ==/2) rather than unifiability (through =/2) of X with elements of L.
  6324.  
  6325.  
  6326. $nthmember(XLN)
  6327.  
  6328.  
  6329.  
  6330.  
  6331.  
  6332.                                     94
  6333.  
  6334.  
  6335.  
  6336.  
  6337.  
  6338.  
  6339.  
  6340.  
  6341.  
  6342.  
  6343.  
  6344.  
  6345.      Succeeds if the N[th.] element of the list L unifies with X.  Fails if
  6346.  
  6347.      N is greater than the length of L.  Either X and L, or L and N, should
  6348.  
  6349.      be instantiated at the time of the call.
  6350.  
  6351.  
  6352. $member2(XL)
  6353.  
  6354.  
  6355.      Checks whether X unifies with any of the actual elements  of  L.   The
  6356.  
  6357.      only difference between this and $member/2 is on lists with a variable
  6358.  
  6359.      tail, e.g. [a, b, c | _ ]: while $member/2 would insert X at  the  end
  6360.  
  6361.      of  such  a  list  if  it  did not find it, $member2/2 only checks for
  6362.  
  6363.      membership but does not insert it into the list if it is not there.
  6364.  
  6365.  
  6366. length(LN)
  6367.  
  6368.  
  6369.      Succeeds if the length of the list L is N.  This predicate  is  deter-
  6370.  
  6371.      ministic  if  L  is  instantiated to a list of definite length, but is
  6372.  
  6373.      nondeterministic if L is a variable or has a variable tail.
  6374.  
  6375.  
  6376. subsumes(XY) Succeeds if the term X subsumes the term Y (i.e. if Y is  an
  6377.  
  6378. instance of X).
  6379.  
  6380.  
  6381. 14.  Pragma Files
  6382.  
  6383.  
  6384.  
  6385.  
  6386.      Users may specify pragma information about  SB-Prolog  programs  in  a
  6387.  
  6388. file called the pragma file for the program.  The pragma file corresponding
  6389.  
  6390. to a Prolog source file foo is a file foo.def which is either in  the  same
  6391.  
  6392. directory as the source file, or is in a subdirectory defs in the directory
  6393.  
  6394. containing the source file.  In other  words,  relative  to  the  directory
  6395.  
  6396.  
  6397.  
  6398.                                     95
  6399.  
  6400.  
  6401.  
  6402.  
  6403.  
  6404.  
  6405.  
  6406.  
  6407.  
  6408.  
  6409.  
  6410.  
  6411. containing  the  source  file foo, the pragma file can be either foo.def or
  6412.  
  6413. defs/foo.def.
  6414.  
  6415.  
  6416.      Pragma information in such a file is  specified  as  a  set  of  facts
  6417.  
  6418. prag/3.   The  first  and  second arguments are, respectively, the name and
  6419.  
  6420. arity of the predicate for which information is being specified.  The third
  6421.  
  6422. argument is the pragma being specified, and can be either a term, or a list
  6423.  
  6424. of terms.  Thus, for example, to specify that an index is to be created  on
  6425.  
  6426. the first argument position for a predicate bar/3 in the file foo, we would
  6427.  
  6428. enter, in a file foo.def, the fact ``prag(bar, 3, index)''  or  ``prag(bar,
  6429.  
  6430. 3, [index])''.
  6431.  
  6432.  
  6433.      The pragma information that may  be  specified  is  limited,  at  this
  6434.  
  6435. point,  to  information  about  indexing.   If an index is to be created on
  6436.  
  6437. argument n of a predicate, the corresponding pragma is a term ``index(n)''.
  6438.  
  6439. In the special case where n = 1, the pragma ``index(1)'' may be abbreviated
  6440.  
  6441. to ``index''.
  6442.  
  6443.  
  6444.  
  6445.  
  6446. Acknowledgements
  6447.  
  6448.  
  6449.      A large number of people were involved, at some time or another,  with
  6450.  
  6451. the  Logic  Programming group at SUNY, Stony Brook, and  deserve credit for
  6452.  
  6453. bringing the SB-Prolog system to its present form.  The following names, in
  6454.  
  6455. particular,  ought  to  be  mentioned:  David  Scott  Warren, Weidong Chen,
  6456.  
  6457. Suzanne Dietrich, Sanjay Manchanda, Jiyang Xu  and  David  Znidarsic.   The
  6458.  
  6459. author  is also grateful to Fernando Pereira for permission to use material
  6460.  
  6461. from the C-Prolog manual for the descriptions of Prolog syntax and many  of
  6462.  
  6463.  
  6464.                                     96
  6465.  
  6466.  
  6467.  
  6468.  
  6469.  
  6470.  
  6471.  
  6472.  
  6473.  
  6474.  
  6475.  
  6476.  
  6477. the builtins.
  6478.  
  6479.  
  6480.  
  6481.  
  6482.  
  6483.  
  6484.  
  6485.  
  6486.  
  6487.  
  6488.  
  6489.  
  6490.  
  6491.  
  6492.  
  6493.  
  6494.  
  6495.  
  6496.  
  6497.  
  6498.  
  6499.  
  6500.  
  6501.  
  6502.  
  6503.  
  6504.  
  6505.  
  6506.  
  6507.  
  6508.  
  6509.  
  6510.  
  6511.  
  6512.  
  6513.  
  6514.  
  6515.  
  6516.  
  6517.  
  6518.  
  6519.  
  6520.  
  6521.  
  6522.  
  6523.  
  6524.  
  6525.  
  6526.  
  6527.  
  6528.  
  6529.  
  6530.                                     97
  6531.  
  6532.  
  6533.  
  6534.  
  6535.  
  6536.  
  6537.  
  6538.  
  6539.  
  6540.  
  6541.  
  6542.  
  6543. Appendix 1Evaluable Predicates of SB-Prolog
  6544.  
  6545.  
  6546.  
  6547.  
  6548.  
  6549.  
  6550.      An entry of ``B'' indicates  a  builtin  predicate,  ``I''  an  inline
  6551.  
  6552. predicate,  and  ``L''  a  library  predicate.   A ``P'' indicates that the
  6553.  
  6554. predicate is handled by the preprocessor during compilation and/or consult-
  6555.  
  6556. ing.
  6557.  
  6558.  
  6559. (L)     _$undefined_pred/1....    6    (I)     =\=/2 ................   32
  6560. (L)     compile/1 ............    7    (I)     </2 ..................   33
  6561. (L)     compile/2 ............    7    (I)     >/2 ..................   33
  6562. (L)     compile/3 ............    7    (I)     =</2 .................   33
  6563. (L)     compile/4 ............    7    (I)     >=/2 .................   33
  6564. (B)     load/1 ...............    8    (B)     floor/2 ..............   33
  6565. (L)     consult/1 ............    9    (B)     floatc/3 .............   34
  6566. (L)     consult/2 ............    9    (B)     exp/2 ................   34
  6567. (P)     :-/1 .................   11    (B)     square/2 .............   34
  6568. (L)     op/3 .................   18    (B)     sin/2 ................   34
  6569. (B)     see/1 ................   26    (I)     `,'/2 ................   35
  6570. (B)     seen .................   26    (I)     `;'/2 ................   35
  6571. (B)     tell/1 ...............   26    (I)     true/0 ...............   35
  6572. (B)     telling/1 ............   26    (I)     =/2 ..................   35
  6573. (B)     told/0 ...............   26    (P)     !/0 ..................   35
  6574. (B)     $exists/1 ............   26    (P)     not/1 ................   36
  6575. (L)     read/1 ...............   27    (P)     ->/2 .................   36
  6576. (L)     write/1 ..............   27    (L)     repeat/0 .............   36
  6577. (L)     display/1 ............   27    (I)     fail/0 ...............   36
  6578. (L)     writeq/1 .............   27    (I)     var/1 ................   36
  6579. (L)     print/1 ..............   27    (I)     nonvar/1 .............   37
  6580. (B)     writename/1 ..........   27    (B)     atom/1 ...............   37
  6581. (B)     writeqname/1 .........   28    (B)     integer/1. ...........   37
  6582. (L)     print_al/2 ...........   28    (B)     real/1 ...............   37
  6583. (L)     print_ar/2 ...........   28    (B)     number/1 .............   37
  6584. (B)     nl/0 .................   28    (B)     atomic/1 .............   37
  6585. (B)     get0/1 ...............   28    (B)     structure/1 ..........   37
  6586. (B)     get/1 ................   29    (L)     functor/3 ............   38
  6587. (B)     put/1 ................   29    (B)     arg/3 ................   38
  6588. (B)     tab/1 ................   29    (L)     =../2 ................   38
  6589. (I)     is/2 .................   31    (B)     name/2 ...............   39
  6590. (L)     eval/2 ...............   32    (P)     call/1 ...............   39
  6591. (I)     =:=/2 ................   32    (B)     conlength/2 ..........   40
  6592.  
  6593.  
  6594.  
  6595.  
  6596.                                     98
  6597.  
  6598.  
  6599.  
  6600.  
  6601.  
  6602.  
  6603.  
  6604.  
  6605.  
  6606.  
  6607.  
  6608. (L)     setof/3 ..............   40    (B)     system/1 .............   59
  6609. (L)     bagof/3 ..............   41    (B)     syscall/3 ............   59
  6610. (B)     ==/2 .................   43    (L)     globalset/1 ..........   61
  6611. (B)     \==/2 ................   43    (L)     gennum/1 .............   61
  6612. (B)     @</2 .................   43    (L)     gensym/2 .............   61
  6613. (B)     @>/2 .................   43    (L)     trace/1 ..............   62
  6614. (B)     @=</2 ................   43    (L)     untrace/1 ............   65
  6615. (B)     @>=/2 ................   44    (L)     spy/1 ................   65
  6616. (B)     compare/3 ............   44    (L)     nospy/1 ..............   65
  6617. (L)     sort/2 ...............   44    (L)     debug/0 ..............   65
  6618. (L)     alloc_perm/2 .........   45    (L)     nodebug/0 ............   65
  6619. (L)     alloc_heap/2 .........   46    (L)     debugging/0 ..........   66
  6620. (L)     $alloc_heap/5 ........   46    (L)     tracepreds/1 .........   66
  6621. (L)     trimbuff/3 ...........   46    (L)     spypreds/1 ...........   66
  6622. (L)     $trim_buff/4 .........   47    (L)     trace/0 ..............   66
  6623. (L)     assert/1 .............   49    (L)     untrace/0 ............   66
  6624. (L)     assert/2 .............   49    (P)     ::-/2 ................   81
  6625. (L)     asserti/2 ............   50    (L)     et/1 .................   86
  6626. (L)     asserta/1 ............   50    (L)     et_star/1 ............   87
  6627. (L)     asserta/2 ............   50    (L)     et_points/1 ..........   87
  6628. (L)     assertz/1 ............   50    (L)     noet/1 ...............   87
  6629. (L)     assertz/2 ............   50    (L)     et_remove/1 ..........   88
  6630. (L)     assert_union/2 .......   51    (L)     et_calls/2 ...........   88
  6631. (L)     $assert/4 ............   51    (L)     count/1 ..............   90
  6632. (L)     $assertf_alloc_t .....   52    (L)     time/1 ...............   90
  6633. (L)     retract/1 ............   52    (L)     nocount/1 ............   90
  6634. (L)     abolish/1 ............   53    (L)     notime/1 .............   91
  6635. (L)     abolish/2 ............   53    (L)     profiling/0 ..........   91
  6636. (L)     call_ref/2 ...........   53    (L)     prof_reset/1 .........   91
  6637. (L)     call_ref/3 ...........   53    (L)     resetcount/1 .........   91
  6638. (L)     $db_new_prref/3 ......   54    (L)     resettime/1 ..........   92
  6639. (L)     $db_assert_fact/5 ....   54    (L)     profile/0 ............   92
  6640. (L)     $db_add_clref/7 ......   55    (L)     noprofile/0 ..........   92
  6641. (L)     $db_call_prref/2 .....   55    (L)     timepreds/1 ..........   92
  6642. (L)     $db_call_prref_s/2 ...   55    (L)     countpreds/1 .........   92
  6643. (L)     $db_get_clauses/3 ....   56    (L)     prof_stats/0 .........   93
  6644. (L)     $db_kill_clause/1 ....   56    (L)     prof_stats/1 .........   93
  6645. (L)     break/0 ..............   57    (L)     $append/3 ............   94
  6646. (B)     abort/0 ..............   57    (L)     $member/2 ............   94
  6647. (B)     save/1 ...............   57    (L)     $reverse/2 ...........   94
  6648. (B)     restore/1 ............   57    (L)     $merge/3 .............   94
  6649. (B)     cputime/1 ............   58    (L)     $absmember/2 .........   94
  6650. (L)     $getenv/2 ............   58    (L)     $nthmember/3 .........   95
  6651. (B)     statistics/0 .........   58    (L)     length/2 .............   95
  6652. (L)     nodynload/2 ..........   58    (L)     subsumes/2 ...........   95
  6653. (B)     symtype/2 ............   59
  6654.  
  6655.  
  6656.  
  6657.  
  6658.  
  6659.  
  6660.  
  6661.  
  6662.                                     99
  6663.  
  6664.  
  6665.  
  6666.  
  6667.  
  6668.  
  6669.  
  6670.  
  6671.  
  6672.  
  6673.  
  6674.  
  6675. Appendix 2Adding Builtins to SB-Prolog
  6676.  
  6677.  
  6678.  
  6679.  
  6680.      Adding a builtin involves writing the C code for the desired case  and
  6681.  
  6682. installing  it  into  the simulator.  The files in the irectory sim/builtin
  6683.  
  6684. contain the C code for the builtin predicates supported by the system.  The
  6685.  
  6686. following procedure is to be followed when adding a builtin to the system:
  6687.  
  6688.  
  6689.  
  6690.  
  6691. IInstalling C Code:
  6692.  
  6693.  
  6694. (1)  Go to the directory sim/builtin.
  6695.  
  6696.  
  6697. (2)  Look at the #defines in the file builtin.h, and  choose  a  number  N1
  6698.  
  6699.      (between  0  and 255) which is not in use to be the builtin number for
  6700.  
  6701.      the new builtin.
  6702.  
  6703.  
  6704. (3)  Add to the file builtin.h the line
  6705.  
  6706.                  #define NEWBUILTIN N1
  6707.  
  6708.  
  6709.  
  6710. (4)  The convention is that the code for builtin will be in a parameterless
  6711.  
  6712.      procedure  named  b_NEWBUILTIN.   Modify the file init_branch.c in the
  6713.  
  6714.      directory sim/builtin by adding these lines:
  6715.  
  6716.                  extern int b_NEWBUILTIN();
  6717.          and
  6718.                  set_b_inst ( NEWBUILTIN, b_NEWBUILTIN );
  6719.  
  6720.  
  6721.      in the appropriate places.
  6722.  
  6723.  
  6724.  
  6725.  
  6726.  
  6727.  
  6728.                                     100
  6729.  
  6730.  
  6731.  
  6732.  
  6733.  
  6734.  
  6735.  
  6736.  
  6737.  
  6738.  
  6739.  
  6740.  
  6741. (5)  The builtins are compiled together  into  one  object  file,  builtin.
  6742.  
  6743.      Update  the  file  Makefile  by appending the name of your object code
  6744.  
  6745.      file at the end of the line ``OBJS = ... '' and insert the appropriate
  6746.  
  6747.      commands to compile your C source file, e.g.:
  6748.  
  6749.          OBJS = [ ... other file names ... ] newbuiltin.o
  6750.  
  6751.           ...
  6752.  
  6753.          newbuiltin.o: $(HS)
  6754.                  cc $(CFLAGS) newbuiltin.c
  6755.  
  6756.  
  6757.  
  6758. (6)  Execute the updated make file to create an updated object  file  buil-
  6759.  
  6760.      tin.
  6761.  
  6762.  
  6763. (7)  Go to the directory sim and execute make to install the new file buil-
  6764.  
  6765.      tin.
  6766.  
  6767.  
  6768.  
  6769.  
  6770. IIInstalling Prolog Code:
  6771.  
  6772.  
  6773.      Assume that the builtin predicate to be added  is  newbuiltin/4.   The
  6774.  
  6775. procedure for installing the Prolog code for this is as follows:
  6776.  
  6777.  
  6778. (1)  Go to the SB-Prolog system directory lib/src, where the Prolog  source
  6779.  
  6780.      for the library routines is kept.
  6781.  
  6782.  
  6783. (2)  Each builtin definition is of the form
  6784.  
  6785.  
  6786.  
  6787.                  pred( ... ) :- '_$builtin'(N).
  6788.  
  6789.  
  6790.  
  6791.  
  6792.  
  6793.  
  6794.                                     101
  6795.  
  6796.  
  6797.  
  6798.  
  6799.  
  6800.  
  6801.  
  6802.  
  6803.  
  6804.  
  6805.  
  6806.  
  6807.      where N is an integer, the builtin number of pred.
  6808.  
  6809.  
  6810. (3)  Create a Prolog source file newbuiltin.P (notice  correspondence  with
  6811.  
  6812.      the name of the predicate being defined) containing the definition
  6813.  
  6814.  
  6815.  
  6816.                  newbuiltin(A,B,C,D) :- '_$builtin'(N1).
  6817.  
  6818.  
  6819.  
  6820.  
  6821.      where N1 is the builtin number of the predicate  newbuiltin,  obtained
  6822.  
  6823.      when installing the C code for the builtin (see above).
  6824.  
  6825.  
  6826. (4)  Compile this Prolog predicate, using the  simulator  and  the  compile
  6827.  
  6828.      predicate, into a file newbuiltin (notice correspondence with the name
  6829.  
  6830.      of the predicate being defined) in the SB-Prolog directory lib.
  6831.  
  6832.  
  6833.  
  6834.  
  6835.  
  6836.  
  6837.  
  6838.  
  6839.  
  6840.  
  6841.  
  6842.  
  6843.  
  6844.  
  6845.  
  6846.  
  6847.  
  6848.  
  6849.  
  6850.  
  6851.  
  6852.  
  6853.  
  6854.  
  6855.  
  6856.  
  6857.  
  6858.  
  6859.  
  6860.                                     102
  6861.  
  6862.  
  6863.  
  6864.  
  6865.  
  6866.  
  6867.  
  6868.  
  6869.  
  6870.  
  6871.  
  6872.  
  6873.                                TABLE OF CONTENTS
  6874.  
  6875.  
  6876.        1 :  Introduction .............................................    1
  6877.        2 :  Getting Started ..........................................    3
  6878.          2.1 :  The Dynamic Loader Search Path .......................    3
  6879.          2.2 :  System Directories ...................................    5
  6880.          2.3 :  Invoking the Simulator ...............................    5
  6881.          2.4 :  Executing Programs ...................................    7
  6882.             2.4.1 :  Compiling Programs ..............................    7
  6883.             2.4.2 :  Loading Byte Code Files .........................    8
  6884.             2.4.3 :  Consulting Programs .............................    9
  6885.          2.5 :  Execution Directives .................................   11
  6886.        3 :  Syntax ...................................................   12
  6887.          3.1 :  Terms ................................................   12
  6888.          3.2 :  Operators ............................................   16
  6889.        4 :  SB-Prolog: Operational Semantics .........................   20
  6890.          4.1 :  Standard Execution Behaviour .........................   20
  6891.          4.2 :  Cuts and If-Then-Else ................................   21
  6892.          4.3 :  Unification of Floating Point Numbers ................   22
  6893.        5 :  Evaluable Predicates .....................................   24
  6894.          5.1 :  Input and Output .....................................   25
  6895.             5.1.1 :  File Handling ...................................   26
  6896.             5.1.2 :  Term I/O ........................................   27
  6897.             5.1.3 :  Character I/O ...................................   28
  6898.          5.2 :  Arithmetic ...........................................   29
  6899.          5.3 :  Convenience ..........................................   35
  6900.          5.4 :  Extra Control ........................................   35
  6901.          5.5 :  Meta-Logical .........................................   36
  6902.          5.6 :  Sets .................................................   40
  6903.          5.7 :  Comparison of Terms ..................................   42
  6904.          5.8 :  Buffers ..............................................   44
  6905.          5.9 :  Modification of the Program ..........................   47
  6906.          5.10 :  Environmental .......................................   56
  6907.          5.11 :  Global Values .......................................   60
  6908.        6 :  Debugging ................................................   62
  6909.          6.1 :  High Level Tracing ...................................   62
  6910.          6.2 :  Low Level Tracing ....................................   66
  6911.        7 :  The Simulator ............................................   67
  6912.          7.1 :  Invoking the Simulator ...............................   67
  6913.          7.2 :  Simulator Options ....................................   68
  6914.          7.3 :  Interrupt Handling ...................................   70
  6915.        8 :  The Compiler .............................................   70
  6916.          8.1 :  Structure of the Compiler ............................   71
  6917.          8.2 :  Invoking the Compiler ................................   71
  6918.          8.3 :  Compiler Options .....................................   73
  6919.          8.4 :  A Note on Coding for Efficiency ......................   74
  6920.             8.4.1 :  Avoiding Creation of Backtrack Points ...........   74
  6921.             8.4.2 :  Minimizing Data Movement Between Registers ......   77
  6922.          8.5 :  Assembly .............................................   77
  6923.  
  6924.  
  6925.  
  6926.  
  6927.  
  6928.  
  6929.  
  6930.  
  6931.  
  6932.  
  6933.  
  6934.  
  6935.  
  6936.  
  6937.  
  6938.        9 :  Modules ..................................................   78
  6939.        10 :  Macros ..................................................   80
  6940.          10.1 :  Defining Macros .....................................   81
  6941.          10.2 :  Macro Expander Options ..............................   82
  6942.        11 :  Extension Tables ........................................   83
  6943.        12 :  Profiling Programs ......................................   88
  6944.        13 :  Other Library Utilities .................................   93
  6945.        14 :  Pragma Files ............................................   95
  6946.  
  6947.      Appendix 1: Evaluable Predicates of SB-Prolog ...................   98
  6948.  
  6949.      Appendix 2: Adding Builtins to SB-Prolog ........................  100
  6950.  
  6951.  
  6952.  
  6953.  
  6954.  
  6955.  
  6956.  
  6957.  
  6958.  
  6959.  
  6960.  
  6961.  
  6962.  
  6963.  
  6964.  
  6965.  
  6966.  
  6967.  
  6968.  
  6969.  
  6970.  
  6971.  
  6972.  
  6973.  
  6974.  
  6975.  
  6976.  
  6977.  
  6978.  
  6979.  
  6980.  
  6981.  
  6982.  
  6983.  
  6984.  
  6985.  
  6986.  
  6987.  
  6988.  
  6989.  
  6990.  
  6991.  
  6992.  
  6993.  
  6994.  
  6995.  
  6996.  
  6997.